CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2010
    Posts
    56

    Process Exit Event Handler

    I am currently working with the .NET Process Class and there is an event handler for when a process you execute from C# closes will fire off of an event.

    Now my process is that my program will execute a java program that will generate a text file. The C# program will detect the closure and open the generated text file. The problem is when I execute this code again instead of one text file opening, two open, and then three, four, etc, etc every time I execute this code.

    My first suspicion was that it was taking into account notepad closing but if I leave notepad open the same issue occurs so this seems to rule out that conclusion.

    Any ideas?

    Code:
                jProcess.StartInfo = processStartInfo;
                jProcess.EnableRaisingEvents = true;
    
                jProcess.Exited += new EventHandler(jProcess_Exited);
                jProcess.Start();
    Here is the method I fire off.

    Code:
            private void jProcess_Exited(object sender, EventArgs e)
            {
                jProcess.EnableRaisingEvents = false;
                Process.Start(file);
            }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Process Exit Event Handler

    In one case you are using the 'jProcess' Process instance; in another case you are using the static 'Process.Start' method. Should you be using an instance value for both?

  3. #3
    Join Date
    Jun 2010
    Posts
    56

    Re: Process Exit Event Handler

    The jProcess is used specifically to start the java program. The static "Process.Start" is used to load the text file, I figured they would run independently.

    Are you talking about having for example a jProcess for the java program and a tProcess for the text program?

    I believe I tried that already, when the method is triggered I did something like the following within the triggered method. Same result though, every subsequent run somehow opens the text file an additional time.

    Code:
            private void jProcess_Exited(object sender, EventArgs e)
            {
                Process tProcess = new Process();
                ProcessStartInfo tProcessStartInfo = new ProcessStartInfo();
                tProcessStartInfo.FileName = fileName;
                tProcess.StartInfo = tProcessStartInfo;
                tProcess.EnableRaisingEvents = true;
                tProcess.Start();
            }
    Last edited by user612345; May 16th, 2011 at 09:41 AM.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Process Exit Event Handler

    Put a breakpoint on the exit event handler and find out why if it's getting called multiple times. Maybe because you don't unhook the event handler each time after it gets called?

    Code:
    private void jProcess_Exited(object sender, EventArgs e)
    {
      jProcess.EnableRaisingEvents = false;
      jProcess.Exited -= new EventHandler(jProcess_Exited);
      Process.Start(file);
    }

  5. #5
    Join Date
    Jun 2010
    Posts
    56

    Re: Process Exit Event Handler

    Unhooking the handler fixed the issue.

    Now when I did a break on the method to see what the sender object contained each call is exactly identical except the only change is:

    jProcess.EnableRaisingEvents = false;

    Which switches from true on the first call to false. On the second call its already false.

    Even the process IDs are identical.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Process Exit Event Handler

    Quote Originally Posted by user612345 View Post
    Unhooking the handler fixed the issue.
    Cool.

  7. #7
    Join Date
    Jun 2010
    Posts
    56

    Re: Process Exit Event Handler

    Cool yes. Still odd why it would perform the behavior in the first place.

    I even created a simple form project and added the following code for a button and same results.

    Code:
            private void button1_Click(object sender, EventArgs e)
            {
                ProcessStartInfo processInfoObj = new ProcessStartInfo();
                processInfoObj.WorkingDirectory = "C:\\";
                processInfoObj.FileName = "test.txt";
    
                processObj.StartInfo = processInfoObj;
                processObj.EnableRaisingEvents = true;
    
                processObj.Exited += new EventHandler(processExit);
                processObj.Start();
            }
            private void processExit(object sender, EventArgs e)
            {
                processObj.EnableRaisingEvents = false;
                Process.Start("C:\\test2.txt");
            }

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Process Exit Event Handler

    It's not odd at all - it's doing what you are telling it to do.

    Each time you click button1_Click, you wire up a new Exited event handler to the processObj object.
    Code:
    processObj.Exited += new EventHandler(processExit);
    After that process has exited you fail to unhook the existing exited event handler before wiring up a new one.

    Move the following two lines out of button1_Click handler and into where you initialize the processObj field (constructor? OnLoad?).
    Code:
    processObj.EnableRaisingEvents = true;
    processObj.Exited += new EventHandler(processExit);

  9. #9
    Join Date
    Jun 2010
    Posts
    56

    Re: Process Exit Event Handler

    Well I feel silly. I see whats happening now. First time I am really working with events.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured