Hi guys!
I need my program to close old instances of it, when a new one comes up. I did my code like this:
public void killInstance()
{
Process[] Denlillehjaelper;
Denlillehjaelper = Process.GetProcessesByName("Den lille hjælper");
foreach (Process MyProcess in Denlillehjaelper)
{
MyProcess.CloseMainWindow();
MyProcess.Close();
}
}
And I call this from later in my code.
I can see it accurately runs this piece of code (i did a messagebox.show - just to check) but it dosent close the old instances of my application.
Maybe someone could help me? :)
Loading
VulpesPosted Feb 8, 2012, 8:52 AM
Sam HobbsPosted Feb 8, 2012, 6:12 PM
Are you sure you need to close the previous instance? Usually single-instance applications are implemented by closing new instances instead of old instances. It is likely that could be done here too.
Since you have the code for the application, a better solution would be to use inter-process communication. If it were me, I would use something more reliable than the process name for finding existing instances. I realize that it is likely good enough for personal use but if it were me I would prefer something more precise even for personal use.
Mick NegendahlPosted Feb 8, 2012, 8:27 AM
Mick NegendahlPosted Feb 8, 2012, 7:31 AM
Mick NegendahlPosted Feb 8, 2012, 7:10 AM
I now tried to replace CloseMainWindow with Kill - same result. Both the new and the old instance keeps running - any ideas?
Please tell if i should post more code.
If i take this pice of code:
Process[] Denlillehjaelper;
Denlillehjaelper = Process.GetProcessesByName("Den lille hjælper");
foreach (Process MyProcess in Denlillehjaelper)
{
MyProcess.CloseMainWindow();
MyProcess.Close();
}
and put it in my form_load - then it exits the application fine, when a new one starts.
But my problem is that it shouldnt always exit, so i have this controlled with some command line arguments, but when i can see that the argument run fine, and runs my method, it means that there is a problem with the code in the method, right?
/Mick
VulpesPosted Feb 8, 2012, 5:55 AM