CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2007
    Posts
    25

    [RESOLVED] Automatic Updates for Program

    I'm needing to be able to update my database program with newer versions. The updates would be via a network, not over internet.

    I've tried to find an example or tutorial but it doesn't seem to be easy to find.

    I would assume that I would need something which does the following:

    Main Program checks version with database or ini file on server.
    Main Programs calls a secondary exe for updating and closes itself.
    Update program downloads new version
    Update program checks that no instances of main program are running
    Update program overwrites main program exe file.
    Update program calls Main program exe and closes itself.

    I'm fairly new at programming C# so I'm really sure how to do some of the things like copying a file via the network (but I can figure it out).

    What I'm stuck with the is whole checking whether the main program is closed so an error doesn't occur when trying to replace it's exe.

    I also don't know if there's something I might be missing.

    Can anyone direct me to some tutorials or code examples?

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: Automatic Updates for Program

    I believe a 'Mutex' can be used to detect existing instances of applications. Give that a google and find out.

    Another thing that some programs do is they open up a socket on a weird port, i.e. they'd open a socket on port 63918. If they can't open a socket on that port, they assume there's another instance running.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Automatic Updates for Program

    Yes Mutex will be the correct way to go about doing this. Many would probably say that you canuse GetProcessesByName as well. Yeah, it would not be incorrect, but I have found that using GetProcessesByName on Windows Vista, does not work. I had to create a Mutex. Do a search on single instance mutex c# in google.

  4. #4
    Join Date
    May 2002
    Location
    India
    Posts
    143

    Re: Automatic Updates for Program

    Hyarion,

    Can you help me with how you implemented the other activities like:

    Main Program checks version with database or ini file on server.
    Main Programs calls a secondary exe for updating and closes itself.
    Update program downloads new version
    Update program checks that no instances of main program are running
    Update program overwrites main program exe file.
    Update program calls Main program exe and closes itself.

    Actually I am also trying to figure out the similar mechanism for my application, something like smart client. The application would be installed on the client side and should be cabable enough to update any patch or hotfix itself.

    Thanks in advance,
    Anupam.

  5. #5
    Join Date
    Oct 2007
    Posts
    25

    Re: Automatic Updates for Program

    Thank you for the help. A Mutex is definately going to be the way. I'm busy experimenting with it at the moment.

    For Anupam kant:

    I havn't implemented all of this yet, but this is how most of it would be done:

    1. Main Program checks version with database/ini file
    I am using the database, so I just use a select statement, something like "SELECT Version_Num FROM Settings" and compare it to Application.ProductVersion.

    2. Main Program calls a secondary exe for updating and closes itself.
    You'll need to include the System.Diagnostics namespace and then use "Process.Start("filename.exe");" to run the program.

    3. The update program at this stage would do the whole mutex thing to check for running copies of the mainprogram and when there are none running, copy the exe file over the existing one. I am copying over a network so I have just used a normal file copy command.

    4. Once the exe has been copied the update program will do a Process.Start command for the main exe and then close itself.

    I will paste my mutex code here once I've completed it so anyone with a similar query can use it.

  6. #6
    Join Date
    Oct 2007
    Posts
    25

    Re: Automatic Updates for Program

    Code for using a Mutex to check whether a program is running in order to replace exe file:

    In the Main Program's code insert the following - please excuse the tab formatting, I can't seem to get it quite right in these forums

    Code:
    [STAThread]
    		
    		public static void Main(string[] args)
    		{
    			//Create Mutex to ensure only one copy of program running.
    			bool ok;
            	System.Threading.Mutex m = new System.Threading.Mutex(true, "accint_db", out ok);
    			
            	if (! ok)
            	{
                	MessageBox.Show("Another copy of the program is already running.");
                	return;
            	}
    		
    			
    			Application.EnableVisualStyles();
    			Application.SetCompatibleTextRenderingDefault(false);
    			Application.Run(new MainForm());
    			
    			GC.KeepAlive(m); //Required to ensure Mutex isn't close prematurely.
    		}
    Code in update program to check for running mutex:

    Code:
    		void DoUpdate()
    		{
    			
    			
    			if (WaitForMutex() == false)
    			{
    				//timeout reached
    				MessageBox.Show("Unable to copy file, Program still running");
    				
    				return;
    				
    			}
    				
    			//place code here to do update of exe.
    		
    		
    		}
    		bool WaitForMutex()
    		{
    			int SleepPeriod = 500;  //sleep period in milliseconds
    			int TimeOut = 10;  //number of loops before timingout.
    			int counter = 0;
    			do
    			{
    				//check every 500ms till program is closed
    				Thread.Sleep(SleepPeriod);
    				counter++;
    				if (counter > TimeOut) return false; //timeout period reached
    			} 
    			while (CheckMutex());
    			return true; //mutex not found, safe to copy
    		}
    		
    		bool CheckMutex()
    		{
    			//checks whether the mutex of main program is currently running
    			bool ok;
            	System.Threading.Mutex m = new System.Threading.Mutex(true, "accint_db", out ok);
            	//close mutex after checking.
            	m.Close();
            	if (! ok)
            	{
                	//Main program is still running
                	return true;
            	}
            	else
            	{
            		//No Mutex found
            		return false;
            	}
    		}
    I have put a timeout in the mutex checking so that it doesn't just sit there waiting for 4 hours for the program to close. Ideally I want to be able to force the main program to close, but this will do for me for now.

    Many thanks to the author of this page:
    http://www.ai.uga.edu/mc/SingleInstance.html
    as it helped me immensely with explaining the Mutex and how to implement it.

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Automatic Updates for Program

    Well Done!

  8. #8
    Join Date
    Mar 2004
    Posts
    223

    Re: Automatic Updates for Program

    How does the main exe actually self-update? Apart from replacing any new dlls...do we start a self installer?

    [If you find my answers useful, please leave your comments and rating for it.]
    Happy Coding!
    My Dev.: MS VS 2005 Version 8.0.50727.42 .NET 2.0.50727
    Mark your answers Tools>>RESOLVED once its answered.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured