System.Diagnostics.Process.Exited event

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • T

    System.Diagnostics.Process.Exited event

    How do you catch the "Exited" event when a process is terminated. In the following code sample the "myProcess_Exit ed" event is never called. Any help would be appreciated.


    using System;
    using System.Diagnost ics;
    using System.Threadin g;

    class ConsoleProcessA pp
    {
    public static void Main()
    {
    try
    {
    Process myProcess;
    myProcess = Process.Start(" Notepad.exe");
    myProcess.Exite d +=new EventHandler(my Process_Exited) ;

    // Display physical memory usage.
    // Discard cached information about the process.
    myProcess.Refre sh();
    // Print working set to console.
    Console.WriteLi ne("Physical Memory Usage: " + myProcess.Worki ngSet.ToString( ));
    // Close process by sending a close message to its main window.
    myProcess.Close MainWindow();
    // Free resources associated with process.
    myProcess.Close ();
    }
    catch(Exception e)
    {
    Console.WriteLi ne("The following exception was raised: ");
    Console.WriteLi ne(e.Message);
    }
    }

    private static void myProcess_Exite d(object sender, EventArgs e)
    {
    Console.WriteLi ne("Event myProcess_Exite d has been called");
    }
    }
  • David Levine

    #2
    Re: System.Diagnost ics.Process.Exi ted event

    Your app has exited before the process you launched has a chance to run and
    exit itself.

    "T" <T@discussions. microsoft.com> wrote in message
    news:B16E2681-E02E-4771-ADFF-898ABC14EBEB@mi crosoft.com...[color=blue]
    > How do you catch the "Exited" event when a process is terminated. In the[/color]
    following code sample the "myProcess_Exit ed" event is never called. Any
    help would be appreciated.[color=blue]
    >
    >
    > using System;
    > using System.Diagnost ics;
    > using System.Threadin g;
    >
    > class ConsoleProcessA pp
    > {
    > public static void Main()
    > {
    > try
    > {
    > Process myProcess;
    > myProcess = Process.Start(" Notepad.exe");
    > myProcess.Exite d +=new EventHandler(my Process_Exited) ;
    >
    > // Display physical memory usage.
    > // Discard cached information about the process.
    > myProcess.Refre sh();
    > // Print working set to console.
    > Console.WriteLi ne("Physical Memory Usage: " +[/color]
    myProcess.Worki ngSet.ToString( ));[color=blue]
    > // Close process by sending a close message to its main window.
    > myProcess.Close MainWindow();
    > // Free resources associated with process.
    > myProcess.Close ();
    > }
    > catch(Exception e)
    > {
    > Console.WriteLi ne("The following exception was raised: ");
    > Console.WriteLi ne(e.Message);
    > }
    > }
    >
    > private static void myProcess_Exite d(object sender, EventArgs e)
    > {
    > Console.WriteLi ne("Event myProcess_Exite d has been called");
    > }
    > }[/color]


    Comment

    • Sunny

      #3
      Re: System.Diagnost ics.Process.Exi ted event

      Hi,

      Set the 'EnableRaisingE vents' property of the process object to true.

      And you can add
      myProcess.WaitF orExit();

      just after myProcess.Close MainWindow();


      Hope that helps
      Sunny

      In article <B16E2681-E02E-4771-ADFF-898ABC14EBEB@mi crosoft.com>,
      T@discussions.m icrosoft.com says...[color=blue]
      > How do you catch the "Exited" event when a process is terminated. In the following code sample the "myProcess_Exit ed" event is never called. Any help would be appreciated.
      >
      >
      > using System;
      > using System.Diagnost ics;
      > using System.Threadin g;
      >
      > class ConsoleProcessA pp
      > {
      > public static void Main()
      > {
      > try
      > {
      > Process myProcess;
      > myProcess = Process.Start(" Notepad.exe");
      > myProcess.Exite d +=new EventHandler(my Process_Exited) ;
      >
      > // Display physical memory usage.
      > // Discard cached information about the process.
      > myProcess.Refre sh();
      > // Print working set to console.
      > Console.WriteLi ne("Physical Memory Usage: " + myProcess.Worki ngSet.ToString( ));
      > // Close process by sending a close message to its main window.
      > myProcess.Close MainWindow();
      > // Free resources associated with process.
      > myProcess.Close ();
      > }
      > catch(Exception e)
      > {
      > Console.WriteLi ne("The following exception was raised: ");
      > Console.WriteLi ne(e.Message);
      > }
      > }
      >
      > private static void myProcess_Exite d(object sender, EventArgs e)
      > {
      > Console.WriteLi ne("Event myProcess_Exite d has been called");
      > }
      > }
      >[/color]

      Comment

      Working...