Stopping a private sub running

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a datagridview with user specified data in it. I then have a sub
handling a button click which then iterates through the data performing
calculations. As this process could be quite lengthy, I would like to be
able to offer "stop" & "restart" options to the user. I'm using VB Express
2005 in a windows forms application.

I have found very little about this on the web, it mostly relates to
system.diagnostics class. Can anyone help me on this?

Many thanks,
James
 
James,

If you do the calculations in a loop you can check for a flag and simply
exit the methods when the flag is set.

You can also do the calculations in a thread and terminate the thread when
the user click a button. I believe though this terminating the thread should
be done in a more controlled way than just killing the thread because it is
possible that terminating the calculations in the middle might leave the
state of the application unstable.
 
Thanks Stoitcho.

In theory, I look for a flag in my calculation loop but the flag is only set
once the user clicks the "stop" button.

Is this right?

Thanks again,
James
 
James,

The problem is the stop button click event won't execute until the
lengthy calculation is done so the flag won't be set until after the
fact. You'll need to run this calculation in another thread.

Brian
 
Or call Application.DoEvents in the calculation loop to let the message loop
breathe.
 
Back
Top