【问题标题】:handle exit event of child process处理子进程的退出事件
【发布时间】:2011-01-14 09:37:56
【问题描述】:

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

【问题讨论】:

    标签: c# .net process console-application


    【解决方案1】:

    您必须致电newCrawler.WaitForExit() 才能停留到子进程完成。然后你可以使用newCrawler.ExitCode 来获得退出值。

    【讨论】:

    • 谢谢,但我需要使用 Exit 事件,因为我不想在进程存在之前阻止应用程序
    • 好的,但是谁让主进程(您的控制台应用程序)仍在运行?你说它退出了……
    • 我已经使用 ManualResetEvent 来防止主控制台应用程序退出。
    • 你能发布更多关于这部分的代码吗?可能通过添加此部分来编辑原始问题会有所帮助。
    • 使用 ManualResetEvent 毫无意义。它与 newCrawler.WaitForExit 做同样的事情,但增加了额外的开销。
    【解决方案2】:

    似乎进程退出处理可能导致应用程序错误。所以应用程序可能已经终止。你能放一个合适的 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 处理程序中创建一个异常,当时应用程序(主进程)也崩溃了。

    您可以尝试在退出处理程序中注释代码吗?

    【讨论】:

    • 我只是在进程中写了Environment.Exit(0)进行测试,但它仍然不起作用,进程的退出事件是用ExitCode = 0触发的,这意味着没有发生异常进程,但为什么它关闭主控制台应用程序?
    • wooow 我很抱歉,在 Exit 事件中运行代码的第一行会引发异常并关闭程序,但我仍然想知道为什么它会关闭控制台应用程序,因为我在单独的线程中运行它使用线程池
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 2019-12-22
    • 2023-04-09
    相关资源
    最近更新 更多