2009-01-05 121 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)。

相關問題