2011-01-14 11 views
2

私はコンソールアプリケーションを持ち、Mainメソッドにあります。次のコードのようなプロセスを開始します。プロセスが存在する場合、プロセスのExistイベントは起動されますが、コンソールアプリケーションも終了しました。プロセスを開始し、そのプロセスの終了イベントで別のプロセスを開始します。子プロセスのexitイベントを処理する

また、プロセス出力がメインコンソールアプリケーションに反映されていることも有線です。


Process newCrawler = new Process(); 
newCrawler.StartInfo = new ProcessStartInfo(); 
newCrawler.StartInfo.FileName = configSection.CrawlerPath; 
newCrawler.EnableRaisingEvents = true; 
newCrawler.Exited += new EventHandler(newCrawler_Exited); 

newCrawler.StartInfo.Arguments = "someArg"; 
newCrawler.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
newCrawler.StartInfo.UseShellExecute = false; 
newCrawler.Start(); 

答えて

1

子プロセスが終了するまでは、newCrawler.WaitForExit()に電話する必要があります。その後、newCrawler.ExitCodeを使用して終了値を設定することができます。

+0

ありがとう、Exitイベントを使用する必要があります。プロセスが存在するまでアプリケーションをブロックしたくないからです。 – Ehsan

+0

いいえ、メインプロセス(コンソールアプリケーション)は引き続き実行していますか?あなたはそれが終了すると言います... –

+0

私はManualResetEventを使用して、メインコンソールアプリケーションが終了しないようにしました。 – Ehsan

1

プロセス出口処理のように見えますが、アプリケーションエラーが発生している可能性があります。したがって、アプリケーションは終了する可能性があります。適切なtry..catchブロックとdebuggを入れて、間違っていることを確認できますか?または(これはMSDNからです)、また、あなたならば、私は気づいたことの一つは、ある一つの引数(ファイル名)

using System; 
using System.Diagnostics; 
using System.ComponentModel; 
using System.Threading; 
using Microsoft.VisualBasic; 

class PrintProcessClass 
{ 

    private Process myProcess = new Process(); 
    private int elapsedTime; 
    private bool eventHandled; 

    // Print a file with any known extension. 
    public void PrintDoc(string fileName) 
    { 

     elapsedTime = 0; 
     eventHandled = false; 

     try 
     { 
      // Start a process to print a file and raise an event when done. 
      myProcess.StartInfo.FileName = fileName; 
      myProcess.StartInfo.Verb = "Print"; 
      myProcess.StartInfo.CreateNoWindow = true; 
      myProcess.EnableRaisingEvents = true; 
      myProcess.Exited += new EventHandler(myProcess_Exited); 
      myProcess.Start(); 

     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("An error occurred trying to print \"{0}\":" + "\n" + ex.Message, fileName); 
      return; 
     } 

     // Wait for Exited event, but not more than 30 seconds. 
     const int SLEEP_AMOUNT = 100; 
     while (!eventHandled) 
     { 
      elapsedTime += SLEEP_AMOUNT; 
      if (elapsedTime > 30000) 
      { 
       break; 
      } 
      Thread.Sleep(SLEEP_AMOUNT); 
     } 
    } 

    // Handle Exited event and display process information. 
    private void myProcess_Exited(object sender, System.EventArgs e) 
    { 

     eventHandled = true; 
     Console.WriteLine("Exit time: {0}\r\n" + 
      "Exit code: {1}\r\nElapsed time: {2}", myProcess.ExitTime, myProcess.ExitCode, elapsedTime); 
    } 

    public static void Main(string[] args) 
    { 

     // Verify that an argument has been entered. 
     if (args.Length <= 0) 
     { 
      Console.WriteLine("Enter a file name."); 
      return; 
     } 

     // Create the process and print the document. 
     PrintProcessClass myPrintProcess = new PrintProcessClass(); 
     myPrintProcess.PrintDoc(args[0]); 
    } 
} 

に合格することを忘れないでくださいコードを、次の試してみてください ライン

newCrawler.Exited += new EventHandler(newCrawler_Exited); 

and see what happens. 

コメントファイル名をパラメータとして渡していないため、プロセスがクラッシュする可能性がありますが、アプリケーションはそのままです(プロセス内で例外が処理されるため)。

ファイル名を渡していない場合、上記のコードは、 myPrintProcess.PrintDoc(args [0]);で破損します。 は、メインプロセス自体から例外をスローします。 Exitハンドラ内にexceptinを作成しようとしましたが、その時点でアプリケーション(メインプロセス)もクラッシュしました。

Exitハンドラ内のコードにコメントすることはできますか?

+0

私は単にテストのためのプロセスでEnvironment.Exit(0)を書きますが、それでも動作しません、プロセスのexitイベントはExitCode = 0で起動されます。つまり、プロセスに例外は発生しませんが、なぜmainコンソールアプリケーション? – Ehsan

+0

wooowだから申し訳ありませんが、Exitイベントのコードの最初の行を実行すると例外が発生し、プログラムを終了しますが、私はまだコンソールアプリケーションを閉じているのでしょうか? – Ehsan

関連する問題