閱讀793 返回首頁    go 阿裏雲 go 技術社區[雲棲]


【進程線程與同步】5.2 避免在同一機器上運行同一程序的多個實例

using System.Diagnostics;
class Program
{
    static void Main()
    {
        if (TestIfAlreadyRunning())
        {
            System.Console.WriteLine("This app is already running!");
        }
        else
        {
            // Here, the entry point of the application.
        }
    }
    static bool TestIfAlreadyRunning()
    {
        Process processCurrent = Process.GetCurrentProcess();
        Process[] processes = Process.GetProcesses();
        foreach (Process process in processes)
        {
            if (processCurrent.Id != process.Id)
            {
                if (processCurrent.ProcessName == process.ProcessName)
                {
                    return true;
                }
            }
        }
        return false;
    }
}

最後更新:2017-04-03 16:59:42

  上一篇:go HDU 4549 矩陣連乘
  下一篇:go FPGA/CPLD簡介