Reputation: 15121
I want to get notified when the calculator is closed. The following code does not work; the Exited
appears even though I have not closed the calculator. What modification do I have to write?
static void Main()
{
using (Process p = new Process())
{
p.StartInfo.FileName = "calc.exe";
p.EnableRaisingEvents = true;
p.Exited += (x, y) => Console.WriteLine("Exited");
p.Start();
p.WaitForExit();
}
}
Upvotes: 2
Views: 91
Reputation: 672
You are using Winsows 10, calculator is UWP app there and should be started with app url typically: Launching a Windows 10 Store app from C# executable
I bet Microsoft coders created kludge to allow run UWP calculator by same way as in previous Windows versions to keep backward compatibility, so, very probably, some proxy process(es) runs UWP calculator and ends immediately, so you have unexpected behaviour.
Also p.WaitForExit()
is used as sync, and p.Exited
as async way to wait process ending and usually they are not used together, you should choose one.
Upvotes: 4