2012-02-24 15 views
2

を検出:FileSystemWatcherは.exeファイルが保存さになっているフォルダを見ているC#が、私は次のコードしているプロセスの終了

private void fileSystemWatcher_Changed(object sender, System.IO.FileSystemEventArgs e) 
    { 
     System.Diagnostics.Process execute = new System.Diagnostics.Process(); 

     execute.StartInfo.FileName = e.FullPath; 
     execute.Start(); 

     //Process now started, detect exit here 

    } 

。そのフォルダに保存されたファイルは正しく実行されます。しかし、開いたexeが閉じると別の関数が起動されるはずです。

これを行う簡単な方法はありますか?

答えて

3

Process.WaitForExit

そしてProcessIDisposableを実装しているのでついでに、あなたが本当に欲しい:

using (System.Diagnostics.Process execute = new System.Diagnostics.Process()) 
{ 
    execute.StartInfo.FileName = e.FullPath; 
    execute.Start(); 

    //Process now started, detect exit here 
} 
+0

このMSDNのページによると、(http://msdn.microsoft.com/en-私たち/ライブラリ/ system.diagnostics.process.exited(v = vs.110).aspx) 'execute.WaitForExit()'が正しく動作するためには 'execute.EnableRaisingEvents = true'を設定する必要があります。 –

1

あなたはProcessオブジェクトに終了しましたイベントにハンドラをアタッチすることができます。イベントハンドラにはlink to the MSDN articleがあります。

+4

'EnableRaisingEvents'をtrueに設定する必要があることに注意してください。 – ken2k

15

イベントに添付します。Process.Exitedイベント。例:

System.Diagnostics.Process execute = new System.Diagnostics.Process();  
execute.StartInfo.FileName = e.FullPath;  
execute.EnableRaisingEvents = true; 

execute.Exited += (sender, e) => { 
    Debug.WriteLine("Process exited with exit code " + execute.ExitCode.ToString()); 
} 

execute.Start();  
関連する問題