newbie & basic multithreading

D

Dan

Hi all, I'm new to threading but I need to show a popup progress window
during some lengthy operation. The UI uses some "engine" classes which
interoperate via events: e.g. let's say I have a worker class: its events
are:
- a progress-step event which is fired periodically during the class work;
its function is to signal the progress of the work and allow user to abort
it. To do this it passes as event args an EventArgs-derived class which has
a boolean flag set by the client to tell the worker it should stop as soon
as possible. The worker checks this flag after firing the event and
terminates its work if abort was requested.
- a work-finished event which is fired when work is done (or aborted).

Now my question is: what is the best approach to launch this class work in a
separate thread and have a responsive progress bar allowing the user to
abort the work? I have tried the following:
- create an instance of the worker class;
- subscribe to its progress event;
- wrap the call to the class' worker method in a WaitCallback, queue it
in the threads pool and call ShowDialog for the progress window;
- the handler for the progress event updates the progress bar unless the
progress window has been closed by user; in this case, it sets the abort
flag so that the worker can terminate.

Anyway with this approach my progress window is not responsive; the progress
is updated but I cannot manage to abort. Any hint?

Thanks to all!
 
J

Jon Skeet [C# MVP]

Dan said:
Hi all, I'm new to threading but I need to show a popup progress window
during some lengthy operation. The UI uses some "engine" classes which
interoperate via events: e.g. let's say I have a worker class: its events
are:
- a progress-step event which is fired periodically during the class work;
its function is to signal the progress of the work and allow user to abort
it. To do this it passes as event args an EventArgs-derived class which has
a boolean flag set by the client to tell the worker it should stop as soon
as possible. The worker checks this flag after firing the event and
terminates its work if abort was requested.
- a work-finished event which is fired when work is done (or aborted).

Now my question is: what is the best approach to launch this class work in a
separate thread and have a responsive progress bar allowing the user to
abort the work? I have tried the following:
- create an instance of the worker class;
- subscribe to its progress event;
- wrap the call to the class' worker method in a WaitCallback, queue it
in the threads pool and call ShowDialog for the progress window;
- the handler for the progress event updates the progress bar unless the
progress window has been closed by user; in this case, it sets the abort
flag so that the worker can terminate.

Anyway with this approach my progress window is not responsive; the progress
is updated but I cannot manage to abort. Any hint?

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

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