O
ozbear
My scenario ws that I had a small group of worker threads
started from the main Application thread, and they have the
backgroundthread property set to true.
After having been started by the main thread, a given worker
thread does some work in a loop and uses the Control.Invoke
method to update the application main form. The control is
set in a thread property before the thread's Start method is
called and the delegate invoked does some updating on the control.
This is the proper way of doing things as far as I know since a thread
should never do any direct updating of a visual element that
is created by another thread.
My problem is that while the thread(s) are running, if the
user clicks on the main form's "X" button to close the form/
terminate the application, the threads so not automagically
die, or at least the application goes to 100% CPU utilisation
waiting for the somethread.IsAlive property to become false.
I would have expected some exception to be thrown within the
thread trying to perform the Control.Invoke method call on
a control (the form) that is closed.
While the Application object has an OnExit event there does not
appear to ba any Application property that can be looked at to
indicate that the Application is terminating. I have had to
resort to introducing a boolean variable set in the Application's
OnExit event handler to true and changing the
while (somethread.IsAlive) loop to...
while (!app_is_terminating && somethread.IsAlive)
to get a clean exit.
Is there a better way?
Regards, Oz
started from the main Application thread, and they have the
backgroundthread property set to true.
After having been started by the main thread, a given worker
thread does some work in a loop and uses the Control.Invoke
method to update the application main form. The control is
set in a thread property before the thread's Start method is
called and the delegate invoked does some updating on the control.
This is the proper way of doing things as far as I know since a thread
should never do any direct updating of a visual element that
is created by another thread.
My problem is that while the thread(s) are running, if the
user clicks on the main form's "X" button to close the form/
terminate the application, the threads so not automagically
die, or at least the application goes to 100% CPU utilisation
waiting for the somethread.IsAlive property to become false.
I would have expected some exception to be thrown within the
thread trying to perform the Control.Invoke method call on
a control (the form) that is closed.
While the Application object has an OnExit event there does not
appear to ba any Application property that can be looked at to
indicate that the Application is terminating. I have had to
resort to introducing a boolean variable set in the Application's
OnExit event handler to true and changing the
while (somethread.IsAlive) loop to...
while (!app_is_terminating && somethread.IsAlive)
to get a clean exit.
Is there a better way?
Regards, Oz