Closing form while processing

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I have a while loop in a win form code. Also a close button on the form
calls Me.Close. How can I let the user close the form using the close button
even though the loop is still running? Right now pressing Close button waits
for loop to finish and then closes the form.

Thanks

Regards
 
Hi

I have a while loop in a win form code. Also a close button on the form
calls Me.Close. How can I let the user close the form using the close button
even though the loop is still running? Right now pressing Close button waits
for loop to finish and then closes the form.

Thanks

Regards

You should move processing of that task to separate thead
(BackgroundWorker, ThreadPool or
your own thread). There is little bit more work involved since there
is always issue of getting results
and displaying them on form.

I would recommend that you use BackgroundWorker since it will change
your code only minimally.
Just intercept FormClosing action and then do your own loop canceling
logic (BackgroundWorker
can support async cancel also).
 
You should move processing of that task to separate thead
(BackgroundWorker, ThreadPool or
your own thread). There is little bit more work involved since there
is always issue of getting results
and displaying them on form.

I would recommend that you use BackgroundWorker since it will change
your code only minimally.
Just intercept FormClosing action and then do your own loop canceling
logic (BackgroundWorker
can support async cancel also).

To build on this, .NET allows you to create a seperate thread and set
the IsBackgroundThread (or something like that) property to allow the
Application to wait for that thread to finish before it dies. This
should allow your form to close, and for the loop to continue on in
the background, and finally the application would die after the
background thread was finished.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
Back
Top