0

I haven't found my exact situation for this situation. I searched on SO and every example seems to want to finish what they are doing first. Is there an event style thread.terminate command that will allow me to close a thread, even if it is currently making a database call? I don't mean like the following:

public void threadhere()
{
    while (!variable)
    {
        do something;
    }
    end thread;
}

I mean more like:

public void threadhere
{
    making a database call and the condition changes while querying database
    so I will stop doing what I am doing and terminate the thread
}
Mafii
  • 7,227
  • 1
  • 35
  • 55
Maderas
  • 231
  • 2
  • 14
  • 3
    Terminating a thread is not the right approach. Telling the thread itself to exit is much better. – Jonathon Reinhart Mar 23 '16 at 12:42
  • That is semantics. My intent should be understood. – Maderas Mar 23 '16 at 12:45
  • Would you really want to terminate your thread while it was for instance updating a database? That could result in a corrupted database. – o_weisman Mar 23 '16 at 12:46
  • It is **querying** the database. It is never updating. – Maderas Mar 23 '16 at 12:46
  • No it's not just semantics. There is a lot of bad code out there which calls TerminateThread which should only be used in extreme circumstances, not part of normal operations. See https://msdn.microsoft.com/en-us/library/windows/desktop/ms686724(v=vs.85).aspx. – Jonathon Reinhart Mar 23 '16 at 12:47
  • I suppose we will have to agree to disagree. My intent is understood. I want my thread to end... – Maderas Mar 23 '16 at 12:48
  • 3
    You can have a look over CancellationToken: http://stackoverflow.com/questions/15067865/how-to-use-the-cancellationtoken-property – Tamas Ionut Mar 23 '16 at 12:51
  • 1
    Your question has been asked and answered here: http://stackoverflow.com/questions/1327102/how-to-kill-a-thread-instantly-in-c – o_weisman Mar 23 '16 at 12:51
  • Neither of those seem to be exactly what I am looking for because they require me to determine when the right time to **Exit** the thread is and I want it to be as soon as the token changes. Thanks though! @o_weisman – Maderas Mar 23 '16 at 12:55
  • 1
    I was referring to the second answer in my link – o_weisman Mar 23 '16 at 13:00
  • If you post an answer here pointing to that, I will give you a check! Also, @Jonathon I see your point. I come from a db mindset on many things. So, to me querying implies no change in the underlying data. In my mind it was semantics. I just wanted to stop my thread. I will be sure to use exit in future questions and/or answers. – Maderas Mar 23 '16 at 13:02
  • Well, how would killing the thread - stop the work in the database? Either the loop loops often and fast (then there is no need to kill the thread as it will terminate in some short time anyway with a cancellation token) or you may be surprised that your real problem is that the database work will NOT "just stop". – TomTom Mar 23 '16 at 13:22
  • @o_weisman After reviewing the code in the second answer, I don't think that this is what I am looking for. If WaitOne() allowed it to continue working while listening to the ManualReset for the call to set, that would be great! But that is not what is happening here. WaitOne() forces the thread to stop execution, and wait for a set or reset method call. Once that is called, it finishes the execution of its code. Am I wrong? If I am, please correct me with an example. Thanks for showing me this, though. I think it will be of great use in the future. – Maderas Mar 23 '16 at 13:56

0 Answers0