Cancelling current activity in progress indicator

  • Thread starter Dmitry Shaporenkov
  • Start date
D

Dmitry Shaporenkov

Hi all,

in my application I want to implement a progress
indicator which has 'Cancel' button allowing to interrupt
the current activity. What is the best way to do such a
thing? I'm considering the following variant:

The progress indicator is created on demand on the
separate thread and stored in the local memory of the
thread that has initiated its creation ('initiating
thread'). The progress indicator also stores a reference
to the initiating thread. When a user clicks 'Cancel'
button, a delegate is executed on the initiating thread
that throws an exception, so that it can be handled
somewhere on the initiating thread.

The purpose of creating progress indicator on a separate
thread is that it will be unable to handle windows
messages (like clicking 'Cancel') if it is created on the
initiating thread.

Am I right or I'm reinventing the wheel and there is an
easier way? I would prefer simpler design, but I'm not
competent enough in WinForms to figure it out.

Thanks in advance,
Dmitry
 
J

Jerry Ham

It sounds like you have that backwards. You normally want to have your GUI
thread NOT be the one that is in a tight loop doing the "work" - you want
your worker thread to be doing that. Then, your cancel button can be in your
GUI thread as it should be and won't have any problem responding to the
click event.

From what it sounds to me like you are trying to do, you would be leaving
most of your GUI unresponsive (can't resize, minimize, won't redraw when
another window covers it temporarily, etc.). Moving the work to the other
thread fixes all of these things.

Jerry
 
H

Herfried K. Wagner [MVP]

Hello,

Dmitry Shaporenkov said:
in my application I want to implement a progress
indicator which has 'Cancel' button allowing to interrupt
the current activity. What is the best way to do such a
thing? I'm considering the following variant:

The progress indicator is created on demand on the
separate thread and stored in the local memory of the
thread that has initiated its creation ('initiating
thread'). The progress indicator also stores a reference
to the initiating thread. When a user clicks 'Cancel'
button, a delegate is executed on the initiating thread
that throws an exception, so that it can be handled
somewhere on the initiating thread.

Untested:

http://www.codeproject.com/cs/miscctrl/progressdialog.asp
 

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