0

I would like to call _myThread.Abort() after my ManualResetEvent blocked a thread. So either I want to check whether the Thread is already blocked, or I want to call a delegate which only calls _myThread.Abort() right after the thread has been paused.

So far i got:

public void MyPause()
{
    if (_myManualResetEvent == null)
        return;
    else
        _myManualResetEvent.Reset();
}
public void MyResume()
{
    if (_myManualResetEvent == null)
        return;
    else
        _myManualResetEvent.Set();
}
public void MyAbort()
{
    if (_myManualResetEvent == null)
        return;
    else
    {
    //???
    }
}

Something like asking _myManualResetEvent.WaitOne(0) does not work, since it simply returns the state of a ManualResetEvent, which may differ to the actual thread state, but I want to know if the thread is already blocked (which implies it has already called WaitOne()). Checking the thread state does not work, since ManualResetEvent uses System.Threading.ThreadState.WaitSleepJoin, like many other functions do.

xamid
  • 440
  • 11
  • 25
  • 1
    If you can arrange for this level of coordination between your threads, you can almost certainly arrange for a cleaner way for the other thread to exit than by calling `Thread.Abort()`. – Damien_The_Unbeliever May 14 '14 at 12:12
  • I'm not sure what you're trying to do. Are you trying to abort a thread that is in the WaitSleepJoin state from another thread? – Kendall Frey May 14 '14 at 12:56
  • @KendallFrey Sort of, I simply want to abort a thread that is currently blocked by a ManualResetEvent. But it may be in state WaitSleepJoin for other reasons. So I need to check whether it's blocked by the ManualResetEvent. What's unclear about this simple question? – xamid May 15 '14 at 12:46
  • @Damien_The_Unbeliever I did'n ask for some 'cleaner' way, in my case it is important that the thread gets aborted, right after its next WaitOne() call, if I called MyAbort(). – xamid May 15 '14 at 12:47
  • I'm not sure why you'd want to abort the thread. Given the information that you've posted, it seems like you could just tell the thread to cancel itself, then set the ManualResetEvent. – Kendall Frey May 15 '14 at 13:07
  • Well, in my opinion mine is the right cicumstance to use Thread.Abort. I simply want to be able to shut down one huge and complicated program down immediately (without potentionally causing a deadlock), otherwise it might mess things up. I cannot insert instructions everywhere to make these threads more intelligent. I didn't ask for anyones opinion weather one should use Thread.Abort with a MRE, but I asked how to use it. – xamid May 20 '14 at 12:14
  • Anyways, since nobody got an answer and I could also not figure out how to invoke WaitOne() into a thread periodically, I think I'll just use Thread.Supend, Thread.Resume, and Thread.Abort instead of this in my case appearently useless ManualResetEvent class. – xamid May 20 '14 at 12:16

0 Answers0