How to check already running exe in c#.net?

痴心易碎 提交于 2019-12-13 14:15:41

问题


What is the procedure to know that when Everytime the user starts the application it will check if an instance is already running. If its not running it will launch it otherwise it will focus to the current one.i have already tried the code for singleton application but its giving lots of errors. its not running properly. can u provide me the othr alternative solution for this??


回答1:


You should use Mutex class, like explained here: Best way to implement singleton in a console application C#?

EDIT: I have this in my code:

class Program
{
    public const string AppName = "...";
    private static readonly Mutex mutex = new Mutex(true, Program.AppName);

    public Program()
    {
        if (!mutex.WaitOne(TimeSpan.Zero, true))
            throw new ApplicationException("Another instance already running");
    }
}



回答2:


You could used a named Mutex.

Named system mutexes are visible throughout the operating system, and can be used to synchronize the activities of processes. You can create a Mutex object that represents a named system mutex by using a constructor that accepts a name.




回答3:


Single Process Instance Object

Creating a Single Instance Application in C#




回答4:


Using a mutex is not applicable if you're using a background worker. If the backgroundWorker is busy, it can have multiple instances.



来源:https://stackoverflow.com/questions/2182385/how-to-check-already-running-exe-in-c-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!