"wg" <wg@hotmail.com> schrieb:
I am attempting to prevent a second instance of my application from
starting. I have the following code at the beginning of Form_Load
If
Process.GetProcessesByName(Process.GetCurrentProce ss.ProcessName).Length 1 Then
Me.Close()
Application.Exit()
Logger.LogMessage("FormLoad", "Second instance of application attempted")
End If
I know that it is going into the If-Then condition because it writes to my
logger. But the 2nd application still starts up. Should I set the startup
to Sub Main? Or can anyone offer suggestions?
I suggest to put the code in the 'Sub Main' instead of the form, because it
doesn't make much sense to load the form if it should not be shown.
Additionally it's not guaranteed that the 'ProcessName' is unique for your
application, especially if you are using a simple name like "Calculator".
Using a mutex will help to avoid this problem:
<URL:http://www.yoda.arachsys.com/csharp/faq/#one.application.instance>
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>