Closing form while processing

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
 
J

Josip Medved

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).
 
R

rowe_newsgroups

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/
 

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