I’m testing the following and trying to understand what it does to then apply it to me live app. The app seems to work with or without .SynchronizingObject = this;. I’ve looked MSDN but could do with an alternative explanation of what this line does and why I need to include it?
private void btRunProcessAndRefresh_Click_1(object sender, EventArgs e) {
//instantiate a new process and tell it where to find file
myProcess =new Process();
myProcess.StartInfo.FileName =@"notepad.exe";
//creates an action to execute when the event exits
myProcess.Exited +=new EventHandler(MyProcessExited);
myProcess.EnableRaisingEvents =true;
//myProcess.SynchronizingObject =this;
elapsedTime = 0;
myProcess.Start();
myTimer =new System.Windows.Forms.Timer();
myTimer.Tick +=new EventHandler(TimerTickEvent);
myTimer.Interval = SLEEP_AMOUNT;
myTimer.Start();
}
private void MyProcessExited(Object source,EventArgs e) {
myTimer.Stop();
}
private void TimerTickEvent(Object myObject,EventArgs myEventArgs) {
myTimer.Stop();
elapsedTime += SLEEP_AMOUNT;
if (elapsedTime > MAXIMUM_EXECUTION_TIME)
myProcess.Kill();
else
myTimer.Start();
}
See MSDN
If you execute your code sequence in a console application than you probably do not need to set the SynchronizingObject; but if you want to access the UI from the Exited event handler, you must set this member.