Terminating threads immediatly upon button click.

Well sooner or later someone is gonna get tired of looking at my threading questions but a ton of progress is being made and I really appreciate all the help, anyways anyone who has read my previous posts can see that my application connects to a MSSQL database and calls a stored proceedure to dump all the data and that data dump is in a thread of its own so that the gui still functions while its dumping the data. Anyways one of the buttons in the GUI is used to disconnect from the database while its performing this dump or atleast that is what I would like it to do. I've already figured out that you have to redfine the thread etc. after its been aborted to start it up agian and aborting it seems to work as long as the entire stored proceedure has been executed using the thread.abort() command. What I would like to do is have the dump code just stop as soon as the disconnect button is clicked instead of having to wait for the code to finish executing than aborting itself, the reason for this is because the application can be exporting upwards of a million data entries which seems to take a decent amount of time and having to wait for it to complete before exiting the thread makes the program appear to hang.

Any help would be much appreciated, I think this will most likely be the last threading question I have.


Answer this question

Terminating threads immediatly upon button click.

  • fMarkf

    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



  • Joel Martinez

    The 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...


  • h1

    Thank you very much that seems to accomplish what I required, still doesn't terminate the Stored Proceedure mid execution but atleast it appears to terminate it to the end user which is all that I really require. Agian I appreciate everyones help with my threading questions.

  • R.K.S.

    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


  • Terminating threads immediatly upon button click.