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

时间:2011-06-17 21:26:37

标签: c# .net windows

  

可能重复:
  What is the correct way to create a single instance application?
  Return to an already open application when a user tries to open a new instance

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

6 个答案:

答案 0 :(得分:109)

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

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

这适用于任何应用程序(任何名称),如果另一个实例运行相同的应用程序,则会true

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

if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1) return;
从您的Main方法

退出方法... OR

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

会立即终止当前加载过程。


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

答案 1 :(得分:51)

我不确定您对“程序”的意思,但如果您想将应用程序限制为一个实例,那么您可以使用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;
        }
    }
}

答案 2 :(得分:12)

以下是一些很好的示例应用程序。以下是一种可能的方式。

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.
}

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

First one.

Second One.

Third One

编辑1:刚刚看到你的评论,你有一个控制台应用程序。这在second sample.

中讨论过

答案 3 :(得分:0)

Process静态类有一个方法GetProcessesByName(),您可以使用它来搜索正在运行的进程。只需搜索具有相同可执行文件名的任何其他进程。

答案 4 :(得分:0)

你可以试试这个

Process[] processes = Process.GetProcessesByName("processname");
foreach (Process p in processes)
{
    IntPtr pFoundWindow = p.MainWindowHandle;
    // Do something with the handle...
    //
}

答案 5 :(得分:-1)

尝试查看This codeplex project以获取实例感知的应用程序。

相关问题