Detecting Whether the Application is Already Running

Posted: December 4, 2007 in .Net
Recently a colegue of mine needed to know how he can determine whether his application is already running in the machine at application start.
 
Using this post I am going to share the solution with you all, showing how you can determine whether your application is already running in the computer. By detecting this either you can stop starting of the second instance of the application and activate the already running application for the user or you can attempt to close the earlier application and start the new one. If you does not check this then multiple applications might start and the user might complain about the application performance. Also this may cause problems occurring from locked resources.
 
* Visual Basic (VB)
Using VB you can easily check  this by using the App object.
————————————————————
If App.PrevInstance = True Then
     MsgBox("The program ‘" & App.EXEName & "’is already running. Please use the running application.")
     End
End If
————————————————————
 
* Visual Basic .NET (VB.NET)
Using VB.NET you can do this by using Diagnostics.Process class.
————————————————————
If Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess().ProcessName).Length > 1 Then
     Application.Exit()
End If
————————————————————
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s