WPF アプリケーションのインスタンスを 1 つだけ実行できるようにするにはどうすればよいですか?
ありがとう。
http://blogs.microsoft.co.il/blogs/arik/archive/2010/05/28/wpf-single-instance-application.aspx
他の例でアドバイスされているように、VB.DLL は必要ありません。WPF サンプル コードがあります。コマンド行引数を初期インスタンスに渡します。
これを試してください:単一インスタンス アプリケーション。私は2番目の方法を使用しましたが、うまく機能します。
このソリューションを確認してください: WPF アプリケーションのインスタンスを 1 つだけ実行できるようにする
これにより、アプリケーションの 1 つのインスタンスが強制されるだけでなく、アプリケーションの追加のインスタンスが実行されたときに、現在のアプリケーションにフォーカスが与えられます。1 つのインスタンスを制限する私のミューテックス ソリューションは、実際には上記のリンクとは異なりますが、このソリューションの「フォーカス」要素が気に入りました。
このヘルパー メソッドを使用して、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