2

I have a console application and in the Main method. I start a process like the code below, when process exists, the Exist event of the process is fired but it closed my console application too, I just want to start a process and then in exit event of that process start another process.

It is also wired that process output is reflecting in my main console application.


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();

Ehsan
  • 1,662
  • 6
  • 28
  • 49

2 Answers2

1

You have to call newCrawler.WaitForExit() in order to stay until the child process finish. Then you can use newCrawler.ExitCode to have the exit value.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • Thanks, but I need to use the Exit event cause I don't want to block the application until the process exists – Ehsan Jan 14 '11 at 09:45
  • Ok, but who's keeping the main process ( your console application ) still running ? You say that it exits... – Felice Pollano Jan 14 '11 at 09:47
  • I have used ManualResetEvent to prevent the main console application from exiting. – Ehsan Jan 14 '11 at 09:47
  • could you post some more code about this part ? May be editing the original question by adding this part will help. – Felice Pollano Jan 14 '11 at 09:49
  • Using ManualResetEvent is pointless. It does the same thing as newCrawler.WaitForExit but adds extra overhead. – dzendras Jan 14 '11 at 09:56
1

Seems like the Process exit handling could have caused the application error. So the application could have terminated. Can you put a proper try..catch block and debugg to see what is going wrong. Or comment the line

newCrawler.Exited += new EventHandler(newCrawler_Exited);  

and see what happens.

Please try following code (This is from MSDN) , also don't forget to pass one argument (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]);
    }
}

One thing I noticed is, if you are not passing the filename as parameter, that will lead the process to crash, but still the application is intact (Since the exception is handled inside the process).

If you are not passing the filename the above code will crash beacuse the myPrintProcess.PrintDoc(args[0]); will throw exception from main process itself. I tried to create an exceptin inside the Exit handler, at that time the application (main process) also crashed.

can you try commenting the code inside Exit handler?

KBBWrite
  • 4,373
  • 2
  • 20
  • 22
  • I just write Environment.Exit(0) in the process for testing but it still don't works, exit event of the process is fired with ExitCode = 0 which means there is no exception occured in the process but why it closes the main console application ? – Ehsan Jan 14 '11 at 09:51
  • wooow im so so sorry, running the first line of the code in the Exit event fires an exception and close the program but im still wonder why it closes the console application cause im running it in a separate thread with ThreadPool – Ehsan Jan 14 '11 at 10:01