Background worker / Progress Bar

G

Guest

Hi,

I'm building an app that performs a long running operation when the user
clicks a button. I'm not sure how long the operation will take up front and
its not an interative task. I have 2 problems at the moment, one is
cancelling the operation and the other is displaying 'progress'. I've decided
to use the BackgroundWorker component in .Net 2.0 but I guess this is still a
relevent 1.1 question.

My first problem is when clicking the run button I change the text of the
button to cancel so when the user clicks it a second time I call the
backgroundWorker CancelAsync. This changes the text and allows the user to
run the process again. The problem is it doesn't seem to cancel the work
being done...instead the work continues and eventually it calls the work
completed method with e.cancelled being true etc but the main problem is not
time is saved....

The second problem is updating progress to the user. Since I have no idea
how long it will take and can't break the task up into steps I thought of
using the progress bar and moving progress up and down just to show something
is happening. Any suggestions for how to do this or a better way to do it?

Thanks
N
 
A

Andy

Neil said:
My first problem is when clicking the run button I change the text of the
button to cancel so when the user clicks it a second time I call the
backgroundWorker CancelAsync. This changes the text and allows the user to
run the process again. The problem is it doesn't seem to cancel the work
being done...instead the work continues and eventually it calls the work
completed method with e.cancelled being true etc but the main problem is not
time is saved....

Your code in the DoWork event handler must check that a cancelation is
pending (CancelPending) and stop doing its work. A cancel won't just
kill the background process, since you may need to do cleanup.
The second problem is updating progress to the user. Since I have no idea
how long it will take and can't break the task up into steps I thought of
using the progress bar and moving progress up and down just to show something
is happening. Any suggestions for how to do this or a better way to do it?

If you don't have a defined number of steps, you can set the progress
bar to marquee. You'll get some green bars that just go left to right
(kinda like the Knight Rider eye, but it only goes left to right).

You should test to make sure this works with visual theme support
turned off or on Win2k.

HTH
Andy
 
G

Guest

Thanks Andy. I take the point about clean up and the need to check the
CancellationPending property. I guess this all works well if you have a
multi-step long running process because you can check after each step. Do you
have any suggestions on how you might achieve the same when the long running
process is just one step?

Thanks
 
A

Andy

If your long running process is just one step, there's no way to really
report progress.

Well, unless the 'long running step' is coded by you, and you could add
some events which you could hook onto in DoWork just before you call
the other step.

I have a long running process which can take a while (its getting data
from a database). There's no way for me to raise events, so I have an
animation run while its working. As a result, I set my background
worker's SupportsCancelation to false (since I can't really cancel it).

So if you can't break out of that one step, you'll just have to live
with the fact that you can't cancel (or make that one step somehow
support cancelation and async. calls, much like Sockets BeginRead and
EndRead).

Andy
 
O

Otis Mukinfus

Hi,

I'm building an app that performs a long running operation when the user
clicks a button. I'm not sure how long the operation will take up front and
its not an interative task. I have 2 problems at the moment, one is
cancelling the operation and the other is displaying 'progress'. I've decided
to use the BackgroundWorker component in .Net 2.0 but I guess this is still a
relevent 1.1 question.

My first problem is when clicking the run button I change the text of the
button to cancel so when the user clicks it a second time I call the
backgroundWorker CancelAsync. This changes the text and allows the user to
run the process again. The problem is it doesn't seem to cancel the work
being done...instead the work continues and eventually it calls the work
completed method with e.cancelled being true etc but the main problem is not
time is saved....

The second problem is updating progress to the user. Since I have no idea
how long it will take and can't break the task up into steps I thought of
using the progress bar and moving progress up and down just to show something
is happening. Any suggestions for how to do this or a better way to do it?

Thanks
N


If you're using .NET 2.0 you can set the progress bar to Marquee Style and it
will just run until you shut it off at the end of your process.

When you load the form have it set to Blocks Style and when you start the
process set it to Marquee Style. When the process ends set it back to Blocks
Style. It's good for processes you don't know the iterations or timing of.

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.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