Created
August 12, 2015 02:59
-
-
Save Diastro/e9e6161e4614bd2e6fbb to your computer and use it in GitHub Desktop.
Event raised twice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| namespace ProcessPrototype | |
| { | |
| using System.Diagnostics; | |
| class Program | |
| { | |
| static void Main(string[] argsd) | |
| { | |
| // Select which scenario to run | |
| ScenarioC(); | |
| } | |
| /// <summary> | |
| /// What we used to see, Exited even isn't fired | |
| /// </summary> | |
| public static void ScenarioA() | |
| { | |
| var p = new Process(); | |
| p.StartInfo.FileName = "notepad.exe"; | |
| p.StartInfo.CreateNoWindow = true; | |
| p.EnableRaisingEvents = true; | |
| p.Exited += new EventHandler(myProcess_Exited); | |
| p.Start(); | |
| Console.ReadLine(); | |
| p.Kill(); | |
| p.Dispose(); | |
| Console.WriteLine("Process disposed."); | |
| Console.ReadLine(); | |
| } | |
| /// <summary> | |
| /// Normal behaviour, Exited event is raised | |
| /// </summary> | |
| public static void ScenarioB() | |
| { | |
| var p = new Process(); | |
| p.StartInfo.FileName = "notepad.exe"; | |
| p.StartInfo.CreateNoWindow = true; | |
| p.EnableRaisingEvents = true; | |
| p.Exited += new EventHandler(myProcess_Exited); | |
| p.Start(); | |
| Console.ReadLine(); | |
| p.Kill(); | |
| if (p.HasExited) | |
| { | |
| p.Dispose(); | |
| } | |
| Console.WriteLine("Process disposed."); | |
| Console.ReadLine(); | |
| } | |
| /// <summary> | |
| /// Event is raised twice | |
| /// </summary> | |
| public static void ScenarioC() | |
| { | |
| var p = new Process(); | |
| p.StartInfo.FileName = "notepad.exe"; | |
| p.StartInfo.CreateNoWindow = true; | |
| p.EnableRaisingEvents = true; | |
| p.Exited += new EventHandler(myProcess_Exited); | |
| p.Start(); | |
| Console.ReadLine(); | |
| p.Kill(); | |
| if (p.WaitForExit(200)) | |
| { | |
| p.Dispose(); | |
| } | |
| Console.WriteLine("Process disposed."); | |
| Console.ReadLine(); | |
| } | |
| private static void myProcess_Exited(object sender, System.EventArgs e) | |
| { | |
| Console.WriteLine("Exit event. Time:" + sender.GetType().ToString()); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment