How process messages in a loop?

  • Thread starter Thread starter Bill
  • Start date Start date
B

Bill

I am copying data from one FileStream to another in a loop. Inside the
loop I display progress messages in a StatusBar. The form has a Stop
button that sets a private Boolean variable to true. Also inside the
loop I check that value of the variable and exit the loop if it is true.

I have two problems. First, clicking the Stop button has no effect.
It's Click event handler does not execute. Second, if I drag the form
to a different location on the screen the progress messages stop
displaying even though I call StatusBar.Refresh in the loop.

What should I call in the loop to allow messages and events to be
processed?
 
Bill said:
I am copying data from one FileStream to another in a loop. Inside the
loop I display progress messages in a StatusBar. The form has a Stop
button that sets a private Boolean variable to true. Also inside the
loop I check that value of the variable and exit the loop if it is true.

I have two problems. First, clicking the Stop button has no effect.
It's Click event handler does not execute. Second, if I drag the form
to a different location on the screen the progress messages stop
displaying even though I call StatusBar.Refresh in the loop.

What should I call in the loop to allow messages and events to be
processed?

You shouldn't. Instead, you should do the processing in a different
thread, crossing back to the UI thread to update the UI with progress.

See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml
 
Hi Bill,

As Jon said, you should use another thread for processing and keep the UI thread free. However, there is a way that will accomplish what you seek (for short tiny applications :). Calling Application.DoEvents() will process pending events.
 
Thanks guys. Application.DoEvents will solve my immidiate problem. I
will rewrite this using threads since I need to learn how to use
threads anyway.
 
Back
Top