Terminating threads immediatly upon button click.
Any help would be much appreciated, I think this will most likely be the last threading question I have.
Any help would be much appreciated, I think this will most likely be the last threading question I have.
You don't need to have the thread abort its self. You can have the UI thread abourt your worker thread.
If you save off an instance to the thread you create in a variable on your form, you can call the abort method on it from your UI thread. This will then kill it immedietly. Something like this should work:
dim t = new Thread(...)
t.Start()
'decide to kill thread
t.Abort()
-Scott Wisniewski
But please check the warnings in the Thread.Abort section. There are good reasons to avoid doing this. The general recommendation when using multiple threads is to *not* use Thread.Abort.
Best regards,
Johan Stenberg
way you describe this, it sounds like the stored procedure is taking a
long time to execute, not your thread. If that is the case, using
Thread.Abort() is not going to work, it won't abort the stored
procedure execution.
As an alternative, consider using the SqlCommand.BeginExecuteNonQuery()
method. It starts the stored procedure and immediately returns. You can
try using the Cancel method to abort the stored procedure. Bonus: you don't have use a thread anymore, your GUI will stay responsive...