Raising process exit event

W

WRH

Hello
I used the VS designer to set up Process code as follows...
the problem is, although the process is executed, when
it is shut down I never get the Exited event.


....
private System.Diagnostics.Process process1;
....
this.process1 = new System.Diagnostics.Process();
....

// process1
//
this.process1.EnableRaisingEvents = true;
this.process1.SynchronizingObject = this;
this.process1.Exited += new System.EventHandler(this.process1_Exited);
....
process1.StartInfo.FileName = @"F:\G\MS Paint";
process1.Start();

....

private void process1_Exited(object sender, System.EventArgs e)
{
MessageBox.Show("Exited");
}
 
W

WRH

Problem solved. It turns out that if the process is a shortcut,
ie .lnk then the process runs but does not raise the event.
 
S

Stoitcho Goutsev \(100\)

WRH,

That should work perfectly. In order to get that event fired you need to set
EnableRaisingEvents = true; I can see you did that.

My suggestion is to double check if this flag is set.

Another case where you may not get the event fired (I just want to make
clear that this is very unlikely and I don't believe this is your problem)
is because you set SynchronizingObject to the parent control. In this case
the code will try to marshal the event call to the UI thread. Internally
this is done using the windows message loop and it should work perfectly.
There might be a catch though. If the parent control is a user control for
example and the control is created using the *new* operator, but not added
to any Controls collection it might be possible that the message loop
doesn't serve these control or the control might be created in a worker
theread that has no message loop at all. Again this is very hypothetical,
but there are chances that you may recognize similar situation in your code.
 
W

WRH

Thanks for the response and the info. I did find this particular problem and
posted this note...

Problem solved. It turns out that if the process is a shortcut,
ie .lnk then the process runs but does not raise the event.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top