See if your .NET application is already running - Create a singleton application

In Visual Basic 6 you could use App.PrevInstance to see if your application was already running then issue the End statement to limit your application to a single instance.

In VB.NET you need to get the name of your main module and look for it in the collection of currently running processes. There are a number of ways to do this. Here is one way:

Imports System.Diagnostics

Dim aModuleName As String = Diagnostics.Process.GetCurrentProcess.MainModule.ModuleName

Dim aProcName As String = System.IO.Path.GetFileNameWithoutExtension(aModuleName)

    If Process.GetProcessesByName(aProcName).Length > 1 Then
        Application.Exit()
    End If

Another way in C# is:

    using System.Diagnostics;

    Process aProcess = Process.GetCurrentProcess();
    string aProcName = aProcess.ProcessName;
			
    if (Process.GetProcessesByName(aProcName).Length > 1)
    {
        Application.ExitThread();
    }



About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page