2011-01-14 131 views
2

我有一个控制台应用程序和Main方法。我开始一个类似下面的代码的进程,当进程存在时,进程的Exist事件被触发,但它也关闭了我的控制台应用程序,我只想启动一个进程,然后在该进程的退出事件中启动另一个进程。处理子进程退出事件

这也是有线这一过程的输出是我的主控制台应用程序反映。


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

谢谢,但我需要使用退出事件,因为我不想阻止该应用程序,直到进程存在 – Ehsan 2011-01-14 09:45:39

+0

好,但谁是保持主处理(控制台应用程序)仍在运行?你说它退出... – 2011-01-14 09:47:01

1

好像进程退出处理可能造成应用程序错误。所以应用程序可能已经终止。你可以把一个适当的try..catch块和调试,看看有什么问题。或注释 线

newCrawler.Exited += new EventHandler(newCrawler_Exited); 

and see what happens. 

请尝试以下代码(这是从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]); 
    } 
} 

有一件事我就是注意到了,如果你没有传递文件名作为参数,这会导致进程崩溃,但应用程序仍然是完整的(因为异常在进程内部处理)。

如果您没有传递文件名,上面的代码会崩溃,因为myPrintProcess.PrintDoc(args [0])是 ; 会从主进程本身抛出异常。 我试图在Exit处理程序中创建一个exceptin,那时应用程序(主进程)也崩溃了。

你可以尝试评论退出处理程序内的代码吗?