Application.Current.Shutdown()を使用して、アプリケーションを停止できます。これは、OnStartupで呼び出された場合、表示されるウィンドウの前に追加できます。
引数を読み取るには、OnStartupまたはEnvironment.GetCommandLineArgs()のe.Argsをどこでも使用できます。
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
// check if it is the first instance or not
// do logic
// get arguments
var cmdLineArgs = e.Args;
if (thisisnotthefirst)
{
// logic interprocess
// do logic
// exit this instance
Application.Current.Shutdown();
return;
}
base.OnStartup(e);
}
protected override void OnExit(ExitEventArgs e)
{
// may be some release needed for your single instance check
base.OnExit(e);
}
}
単一のインスタンスをどのようにチェックするかはわかりませんが、Mutexを使用しています:
protected override void OnStartup(StartupEventArgs e)
{
Boolean createdNew;
this.instanceMutex = new Mutex(true, "MySingleApplication", out createdNew);
if (!createdNew)
{
this.instanceMutex = null;
Application.Current.Shutdown();
return;
}
base.OnStartup(e);
}
protected override void OnExit(ExitEventArgs e)
{
if (this.instanceMutex != null)
{
this.instanceMutex.ReleaseMutex();
}
base.OnExit(e);
}
それがあなたを助けることを願っています。