如何检查应用程序的另一个实例是否正在运行

Tom*_*Tom 40 .net c# windows

可能重复:
创建单实例应用程序的正确方法是什么?
当用户尝试打开新实例时,返回已打开的应用程序

有人可以显示如何检查程序的另一个实例(例如test.exe)是否正在运行,如果是,则停止加载应用程序(如果有现有实例).

Ver*_*cas 117

想要一些严肃的代码?这里是.

var exists = System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1;
Run Code Online (Sandbox Code Playgroud)

这适用于任何应用程序(任何名称),true如果有另一个实例运行相同的应用程序,它将成为.

编辑:为了满足您的需求,您可以使用以下任一方法:

if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1) return;
Run Code Online (Sandbox Code Playgroud)

从您的Main方法退出方法...或

if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1) System.Diagnostics.Process.GetCurrentProcess().Kill();
Run Code Online (Sandbox Code Playgroud)

这将立即杀死当前加载的进程.


您需要为扩展方法添加对System.Core.dll的引用.或者,您也可以使用酒店..Count() .Length


Pat*_*son 55

它不确定你对"程序"的意思,但是如果你想将你的应用程序限制在一个实例上,那么你可以使用Mutex来确保你的应用程序还没有运行.

[STAThread]
static void Main()
{
    Mutex mutex = new System.Threading.Mutex(false, "MyUniqueMutexName");
    try
    {
        if (mutex.WaitOne(0, false))
        {
            // Run the application
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
        else
        {
            MessageBox.Show("An instance of the application is already running.");
        }
    }
    finally
    {
        if (mutex != null)
        {
            mutex.Close();
            mutex = null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 为什么选择downvote?这毫无疑问地回答了这个问题.请解释为什么这不是一个令人满意的答案. (3认同)
  • 这是正确的答案,其他答案对前不起作用。如果我用另一个名称复制exe。 (3认同)
  • @TamusJRoyce不,如果您阅读MSDN文档,它会明确指出如果放弃Mutex,下一个等待的线程将获得它的所有权. (2认同)
  • 当第二个实例正在运行时,“将您的应用程序限制为一个实例”会产生错误 - 它只是没有走得太远。仍然是检测另一个实例是否正在运行的好方法。 (2认同)

Cha*_*thJ 13

这是一些很好的示例应用程序.以下是一种可能的方法.

public static Process RunningInstance() 
{ 
    Process current = Process.GetCurrentProcess(); 
    Process[] processes = Process.GetProcessesByName (current.ProcessName); 

    //Loop through the running processes in with the same name 
    foreach (Process process in processes) 
    { 
        //Ignore the current process 
        if (process.Id != current.Id) 
        { 
            //Make sure that the process is running from the exe file. 
            if (Assembly.GetExecutingAssembly().Location.
                 Replace("/", "\\") == current.MainModule.FileName) 

            {  
                //Return the other process instance.  
                return process; 

            }  
        }  
    } 
    //No other instance was found, return null.  
    return null;  
}


if (MainForm.RunningInstance() != null)
{
    MessageBox.Show("Duplicate Instance");
    //TODO:
    //Your application logic for duplicate 
    //instances would go here.
}
Run Code Online (Sandbox Code Playgroud)

许多其他可能的方式.请参阅示例以了解替代方案

第一.

第二个.

第三个

编辑1:刚刚看到你的评论,你有一个控制台应用程序.这在第二个样本中讨论.