user1860934
user1860934

Reputation: 427

Catch Process.Exited id

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

Answers (1)

Douglas
Douglas

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

Related Questions