Non-responsive

  • Thread starter Thread starter Jim Christiano
  • Start date Start date
J

Jim Christiano

I've created a C# window's form application that migrates data. As it
migrates data, the program itself is non-responsive. I would like to
implement a cancel button to stop the migration mid-stream. Will I have to
use threading to split the tasks or is there another way?

Thanks in advance,
Jim Christiano
 
Your best bet is going to be threading.

Simply spin up a thread to do the work, and have a delegate so that your GUI
can be updated on the proper thread. This will leave your cancel button
responsive in the GUI thread. The worker thread will need to monitor that,
so you'll want some variable or object that the cancel button will set to a
value that the worker thread will check for. (unless the cancel button just
"aborts" the thread - depends on what the migration is doing).


Jerry
 
Although, threading is the recommended (i.e. most
efficient and most elegant) way to go there is an easier
way.

I'm assuming that your application is performing
processing in some sort of loop so the DoEvents method can
be placed at strategic location(s) in your code to respond
to user input.

For example, if you have a cancel button that sets a
module level boolean variable then you could do something
like this:

Do While <some condition>
<perform a unit of processing>
DoEvents
If m_bCancel = True Then
'The cancel button has been pressed.
Exit Do
End If
Loop

The more frequently your program executes the DoEvents the
more "responsive" (i.e. faster) your program will be at
processing the button click event.

Caveat, this is much less efficient than using a thread so
you would only want to do this in simple utilities and use
threading in production level applications.
 
Back
Top