Reputation: 427
How can i catch my process id when the event raised ?
Process pros = Process.Start(ProcessStartInfo);
pros.EnableRaisingEvents = true;
pros.Exited += pros_Exited;
private void pros_Exited(object sender, EventArgs e)
{
int processId = ??
}
Upvotes: 0
Views: 829
Reputation: 54877
You can use an anonymous function the captures the Process
variable:
Process pros = Process.Start(processStartInfo);
pros.EnableRaisingEvents = true;
pros.Exited += (object sender, EventArgs e) =>
{
int processId = pros.Id;
// ...
};
Edit: If you intend to use the above notation in a loop, make sure that you understand closures.
foreach (Process process in myProcesses)
{
process.EnableRaisingEvents = true;
Process processInner = process; // copy to inner variable
processInner.Exited += (object sender, EventArgs e) =>
{
int processId = processInner.Id; // always reference inner variable
// ...
};
}
Upvotes: 3