[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?
[1153 byte] By [hyarion] at [2007-11-20 11:44:14]
# 1 Re: [RESOLVED] 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.
Mutant_Fruit at 2007-11-9 11:37:01 >
# 2 Re: [RESOLVED] 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.
HanneSThEGreaT at 2007-11-9 11:38:02 >
# 3 Re: [RESOLVED] 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 kant at 2007-11-9 11:39:02 >
# 4 Re: [RESOLVED] 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.
hyarion at 2007-11-9 11:40:08 >
# 5 Re: [RESOLVED] 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 :(

[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:

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.
hyarion at 2007-11-9 11:41:07 >
# 6 Re: [RESOLVED] Automatic Updates for Program
Well Done! :thumb:
HanneSThEGreaT at 2007-11-9 11:42:06 >
# 7 Re: [RESOLVED] Automatic Updates for Program
How does the main exe actually self-update? Apart from replacing any new dlls...do we start a self installer?
Charu0306 at 2007-11-9 11:43:11 >