如何避免多个应用程序实例?

时间:2023-01-18 18:18:14

I have an application, "myprogram.exe", which calls functions and code inside a dll, one of this functions that "myprogram.exe" calls create a new instance of a winform, "MyForm.cs" and then show it using form.show();.

我有一个应用程序,“myprogram.exe”,它调用dll中的函数和代码,其中一个函数“myprogram.exe”调用创建一个winform的新实例,“MyForm.cs”,然后使用表单显示它。节目();。

I can have 'n' number of "myprogram.exe" instances running, but I want to have only one instance of "MyForm.cs" for each instance of "myprogram.exe".

我可以运行'n'个“myprogram.exe”实例,但我想为“myprogram.exe”的每个实例只有一个“MyForm.cs”实例。

The problem that I have is that even thought I'm using mutex inside "MyForm.cs" to create a mutex and them ask if an instance of it is already running, sometimes, it creates another instance, despite the mutex.

我遇到的问题是,我甚至认为我在“MyForm.cs”中使用互斥锁来创建一个互斥锁,并询问它的实例是否已经在运行,有时它会创建另一个实例,尽管是互斥锁。

Is there another way that I can use to validate if an instance of "myprogram.exe" has already created an instance of "MyForm.cs".

有没有其他方法可以用来验证“myprogram.exe”的实例是否已经创建了“MyForm.cs”的实例。

2 个解决方案

#1


As per @Joe's comment, the problem is likely to be in the implementation of the Mutux.

根据@ Joe的评论,问题可能出在Mutux的实现上。

This answer to another question demonstrates the right way to do it:

对另一个问题的回答表明了正确的方法:

K. Scott Allen has a good write up on using a Mutex for this purpose and issues you'll run into with the GC.

K. Scott Allen在为此目的使用Mutex方面做了很好的准备,并且您将遇到GC的问题。

If I want to have only one instance of the application running across all sessions on the machine, I can put the named mutex into the global namespace with the prefix “Global\”.

如果我想在机器上的所有会话中只运行一个应用程序实例,我可以将命名的互斥锁放入带有前缀“Global \”的全局命名空间。

[STAThread]
static void Main() 
{
   using(Mutex mutex = new Mutex(false, "Global\\" + appGuid))
   {
      if(!mutex.WaitOne(0, false))
      {
         MessageBox.Show("Instance already running");
         return;
      }

Application.Run(new Form1());
   }
}

#2


I post below link, since I could not find any related article in C++ and MFC. Therefore, For C++, MFC and Win32 you can use http://flounder.com/nomultiples.htm

我发布了以下链接,因为我在C ++和MFC中找不到任何相关文章。因此,对于C ++,MFC和Win32,您可以使用http://flounder.com/nomultiples.htm

#1


As per @Joe's comment, the problem is likely to be in the implementation of the Mutux.

根据@ Joe的评论,问题可能出在Mutux的实现上。

This answer to another question demonstrates the right way to do it:

对另一个问题的回答表明了正确的方法:

K. Scott Allen has a good write up on using a Mutex for this purpose and issues you'll run into with the GC.

K. Scott Allen在为此目的使用Mutex方面做了很好的准备,并且您将遇到GC的问题。

If I want to have only one instance of the application running across all sessions on the machine, I can put the named mutex into the global namespace with the prefix “Global\”.

如果我想在机器上的所有会话中只运行一个应用程序实例,我可以将命名的互斥锁放入带有前缀“Global \”的全局命名空间。

[STAThread]
static void Main() 
{
   using(Mutex mutex = new Mutex(false, "Global\\" + appGuid))
   {
      if(!mutex.WaitOne(0, false))
      {
         MessageBox.Show("Instance already running");
         return;
      }

Application.Run(new Form1());
   }
}

#2


I post below link, since I could not find any related article in C++ and MFC. Therefore, For C++, MFC and Win32 you can use http://flounder.com/nomultiples.htm

我发布了以下链接,因为我在C ++和MFC中找不到任何相关文章。因此,对于C ++,MFC和Win32,您可以使用http://flounder.com/nomultiples.htm