私のWPFプログラムは、SingleInstance.csを使用して、ユーザーがそのショートカットまたはその他のメカニズムをダブルクリックして再起動しようとした場合に、そのインスタンスの1つだけが実行されていることを確認します。ただし、私のプログラムには、それ自体を再起動する必要がある状況があります。再起動を実行するために使用するコードは次のとおりです。
App.Current.Exit += delegate( object s, ExitEventArgs args ) {
if ( !string.IsNullOrEmpty( App.ResourceAssembly.Location ) ) {
Process.Start( App.ResourceAssembly.Location );
}
};
// Shut down this application.
App.Current.Shutdown();
これはほとんどの場合機能しますが、問題は常に機能するとは限らないことです。私の推測では、一部のシステムでは、最初のインスタンスとRemoteService
それが作成するインスタンスがまだ終了していないため、呼び出しによって開始されたプロセスがProcess.Start( App.ResourceAssembly.Location );
終了します。
app.xaml.csのMainメソッドは次のとおりです。
[STAThread]
public static void Main() {
bool isFirstInstance = false;
for ( int i = 1; i <= MAXTRIES; i++ ) {
try {
isFirstInstance = SingleInstance<App>.InitializeAsFirstInstance( Unique );
break;
} catch ( RemotingException ) {
break;
} catch ( Exception ) {
if ( i == MAXTRIES )
return;
}
}
if ( isFirstInstance ) {
SplashScreen splashScreen = new SplashScreen( "splashmph900.png" );
splashScreen.Show( true );
var application = new App();
application.InitializeComponent();
application.Run();
SingleInstance<App>.Cleanup();
}
}
プログラムで再起動している場合に、このコードを取得して新しいインスタンスを開始できるようにする正しい方法は何ですか?bool
フラグを追加し、呼び出しがtrueを返すまでループでRestarting
待機する必要がありますか?それとももっと良い方法はありますか?for
SingleInstance<App>.InitializeAsFirstInstance