Tuesday, December 04, 2007

Detecting Whether the Application is Already Running

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

No comments: