Single Instance Application



sometimes you want to allow the user to run only one instance of your application , so you will need to check if your application is already running at the start up by using the following method which will return null if there is no previous instance is already running otherwise it will return a Process object pointing to that instance ... enjoy

public static Process PriorProcess()
{
    Process curr = Process.GetCurrentProcess();

    Process[] procs = Process.GetProcessesByName(curr.ProcessName);
    foreach (Process p in procs)
    {
        if ((p.Id != curr.Id) && (p.MainModule.FileName == curr.MainModule.FileName))
        return p;
    }
    
    return null;
}

No comments:

Post a Comment