2009-01-05 78 views

回答

15

试试这个:Single instance application。我使用了第二种方法,它工作正常。

+1

第二种方法是我找到的最好的方法。只要克服它使用Microsoft.VisualBasic DLL的事实,它会完成你需要的一切 - 包括正确使用.NET Remoting(不需要套接字或互斥体来创建自己)。但最重要的是,它可以很容易地将参数传递给已经运行的应用程序,或者当您尝试重新打开它时简单地将它带到前面 – 2010-09-27 22:25:07

0

退房这一解决方案:Allowing only one instance of a WPF application to execute

这不仅强制执行的应用程序的一个实例,但它也给你目前的应用重点在应用程序的其他实例中运行。我的mutex解决方案限制一个实例实际上与上面的链接不同,但我喜欢这个解决方案的“焦点”元素。

2

我用这个helper方法,并从application.startup事件

Public Sub ForceSingleInstanceApplication() 
     'Get a reference to the current process 
     Dim MyProc As Process = Process.GetCurrentProcess 

     'Check how many processes have the same name as the current process 
     If (Process.GetProcessesByName(MyProc.ProcessName).Length > 1) Then 
      'If there is more than one, it is already running 
      MsgBox("Application is already running", MsgBoxStyle.Critical, My.Application.Info.Title) 'Reflection.Assembly.GetCallingAssembly().GetName().Name) 
      ' Terminate this process and give the operating system the specified exit code. 
      Environment.Exit(-2) 
      Exit Sub 
     End If 
    End Sub 
0

用户sobelitothis后,它具有以下update调用它。它所说的是,对于更新的资源,您应该使用Windows 7 Taskbar Single Instance,如果您查看源代码将允许您执行所需的操作。

您可以使用SingleInstance c#项目。它还包含WinForms和WPF的样本。

请注意,它也是在Apache 2.0许可下发布的,与Microsoft博客中的Arik的Poznanski发布不同,它不是商业可用的(IANAL,AFAIK)。

相关问题