How do I prevent my app from launching over and over again

This question already has an answer here:

  • What is the correct way to create a single-instance application? 35 answers

  • this is from this answer which is a duplicate of this one. You check in your app's entry constructor for whether or not the app is already running.

    static void Main() 
    {
        Process currentProcess = Process.GetCurrentProcess();
        var runningProcess = (from process in Process.GetProcesses()
                              where
                                process.Id != currentProcess.Id &&
                                process.ProcessName.Equals(
                                  currentProcess.ProcessName,
                                  StringComparison.Ordinal)
                              select process).FirstOrDefault();
        if (runningProcess != null)
        {
            ShowWindow(runningProcess.MainWindowHandle, SW_SHOWMAXIMIZED);
           return; 
        }
    }
    

    create some form of Global Mutex/Event handle that your app creates and then in your open routine check for it being present, if it is, exit the startup routing.

    I have some basic code here in this answer I gave (for a different issue), but it demonstrates what you are trying to do.

    UnauthorizedAccessException on Openexisting global mutex

    链接地址: http://www.djcxy.com/p/51156.html

    上一篇: 使用Mutex运行应用程序的单个实例

    下一篇: 如何防止我的应用一次又一次启动