一次只能有一个程序

时间:2012-07-03 03:24:19

标签: c# concurrency semaphore

我遇到的情况是我只需要同时运行一个程序实例。

这样做很简单:

class OneAtATimePlease
{
  static void Main()
  {
    // Naming a Mutex makes it available computer-wide. Use a name that's
    // unique to your company and application (e.g., include your URL).

    using (var mutex = new Mutex (false, "oreilly.com OneAtATimeDemo"))
    {
      // Wait a few seconds if contended, in case another instance
      // of the program is still in the process of shutting down.

      if (!mutex.WaitOne (TimeSpan.FromSeconds (3), false))
      {
        Console.WriteLine ("Another app instance is running. Bye!");
        return;
      }
      RunProgram();
    }
  }

  static void RunProgram()
  {
    Console.WriteLine ("Running. Press Enter to exit");
    Console.ReadLine();
  }
}

除了细节之外,我需要现有流程终止,而不是新流程。

我尝试制作一个信号量,现有的进程可以在抓取上面的互斥量之后听,但因为我希望它等待信号量然后我最终会遇到信号量总是发出信号的情况,因此它不会工作

任何人都对如何解决这个问题有个好主意?

3 个答案:

答案 0 :(得分:1)

您需要进程间通信,才能向现有应用程序发送信号。对于C#,请参阅IPC Mechanisms in C# - Usage and Best Practices

答案 1 :(得分:1)

Cine,我写过两个Mutex逻辑。第一个是"执行锁定",而第二个是"监视器锁定"。 当第一个进程无法获取监视器锁时,它将退出并释放新进程的执行锁

我不确定这是否是最佳解决方案,欢迎任何反馈。

C#:

class Program
    {
        private static string processName = DateTime.Now.ToString("hh.mm.ss");
        private static bool exitProcess;

        private static Mutex firstLock
        {
            get
            {
                return new Mutex(false, "stackoverflow.com/questions/11304052/");
            }
        }

        private static Mutex secondLock
        {
            get
            {
                return new Mutex(false, "stackoverflow.com/questions/11304052/ #2");
            }
        }


        static void Main(string[] args)
        {
            Console.WriteLine(string.Format("Process {0} starting", processName));

            exitProcess = false;

            while (true)
            {
                using (firstLock)
                {
                    Console.WriteLine(string.Format("Process {0} trying to get #1 mutex", processName));

                    if (!firstLock.WaitOne(TimeSpan.FromSeconds(1), false))
                    {
                        Console.WriteLine(string.Format("Process {0} #1 mutex in use, waiting for release", processName));

                        bool killFirstApp = false;

                        while (!killFirstApp)
                        {
                            killFirstApp = LockSecondMutex();
                        }

                        continue;
                    }

                    new Thread(MonitorSecondMutex).Start();

                    RunProgram();

                    firstLock.ReleaseMutex();

                    break;
                }
            }
        }

        static void RunProgram()
        {
            while (!exitProcess)
            {
                Console.WriteLine(string.Format("Process {0} running", processName));

                Thread.Sleep(1000);
            }
        }

        static void MonitorSecondMutex()
        {
            while (true)
            {
                using (secondLock)
                {
                    if (!secondLock.WaitOne(TimeSpan.FromSeconds(2), false))
                    {
                        Console.WriteLine(string.Format("Process {0} lost second mutex. Will now exit.", processName));

                        exitProcess = true;

                        break;
                    }

                    secondLock.ReleaseMutex();
                }

                Thread.Sleep(500);
            }
        }

        static bool LockSecondMutex()
        {
            while (true)
            {
                using (secondLock)
                {
                    if (!secondLock.WaitOne(TimeSpan.FromSeconds(2), false))
                    {
                        continue;
                    }

                    Thread.Sleep(5000);

                    secondLock.ReleaseMutex();
                }

                return true;
            }
        }
    }

答案 2 :(得分:0)

您可以通过请求访问某些有限的系统范围资源(例如端口)来完成此操作。您的应用程序可以在启动时将套接字绑定到特定端口。如果绑定失败,请向正在运行的实例发送终止信号,然后重试。