Skip to content

Instantly share code, notes, and snippets.

@Diastro
Created August 12, 2015 02:59
Show Gist options
  • Select an option

  • Save Diastro/e9e6161e4614bd2e6fbb to your computer and use it in GitHub Desktop.

Select an option

Save Diastro/e9e6161e4614bd2e6fbb to your computer and use it in GitHub Desktop.
Event raised twice
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