处理子进程的退出事件

时间:2011-01-14 09:37:57

标签: c# .net process console-application

我有一个控制台应用程序和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();

2 个答案:

答案 0 :(得分:1)

您必须致电newCrawler.WaitForExit()才能继续,直到子流程完成。然后,您可以使用newCrawler.ExitCode来获得退出值。

答案 1 :(得分:1)

似乎Process exit处理可能导致应用程序错误。因此应用程序可以终止。你能把一个正确的try..catch块和debugg看看出了什么问题。或评论 行

newCrawler.Exited += new EventHandler(newCrawler_Exited);  

and see what happens.

请尝试以下代码(这是来自MSDN),也不要忘记传递一个参数(FileName)

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(参数[0]); 将从主进程本身抛出异常。 我试图在Exit处理程序中创建一个exceptin,那时应用程序(主进程)也崩溃了。

你可以尝试在Exit处理程序中注释代码吗?