How to enable escape key and a progress bar during long routine.

B

Bill

I have a Windows Form app written with .Net 2.0 that has a lengthy
routine. I would like to:

1. cancel the routine with the Escape key
2. update a progress bar during the routine.
3. avoid using Application.DoEvents which I understand should be
avoided.
4. not use Win32 API calls

asynchronous execution: I do not believe it is possible to update
the progress bar, but I can cancel the routine when the user hits the
Esc key without using DoEvents.

Run the routine in the same thread as the UI: I can get the progress
bar to update, but I cannot enable the Esc key without
Application.DoEvents -can that be done?

What are the options and best practices for this scenario?

Bill
 
P

Pavel Minaev

I have a Windows Form app written with .Net 2.0 that has a lengthy
routine. I would like to:

 1. cancel the routine with the Escape key
 2. update a progress bar during the routine.
 3. avoid using Application.DoEvents which I understand should be
avoided.
 4. not use Win32 API calls

asynchronous execution:   I do not believe it is possible to update
the progress bar, but I can cancel the routine when the user hits the
Esc key without using DoEvents.

It is perfectly possible to update the progress bar from another
thread, if you use Control.Invoke. However, this is not the best
approach; see below.
Run the routine in the same thread as the UI:  I can get the progress
bar to update, but I cannot enable the Esc key without
Application.DoEvents -can that be done?

When doing this (without DoEvents), you also block user input. Also,
if you are doing any blocking calls, you can't stick DoEvents inside
those, so you'll still get UI freezing up occasionally.
What are the options and best practices for this scenario?

Have a look at class BackgroundWorker in MSDN. It is designed
specifically for such scenarios in mind, and has extensive
documentation and sample coverage.
 
F

Family Tree Mike

Bill said:
I have a Windows Form app written with .Net 2.0 that has a lengthy
routine. I would like to:

1. cancel the routine with the Escape key
2. update a progress bar during the routine.
3. avoid using Application.DoEvents which I understand should be
avoided.
4. not use Win32 API calls

asynchronous execution: I do not believe it is possible to update
the progress bar, but I can cancel the routine when the user hits the
Esc key without using DoEvents.

Run the routine in the same thread as the UI: I can get the progress
bar to update, but I cannot enable the Esc key without
Application.DoEvents -can that be done?

What are the options and best practices for this scenario?

Bill


You should look into the BackgroundWorker class. It has a property called
WorkerSupportsCancellation, which then allows you to call CancelAsync on the
escape key hit. It also allows for a ProgressChanged event that you can
monitor and use to update your progress bar. There are plenty of
BackgroundWorker examples available.
 

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