문제

I have a query regarding these two working in conjunction with one another as I am not convinced they go hand in hand.

I have some code which uses the background worker and within DoWork() it performs some logic. This logic simply consumes the TcpClient object and sends data to an external device.

My question is within DoWork() I call AutoResetEvent.WaitOne(). Does this stop the current thread until AutoResetEvent.Set() is called? If for example, AutoResetEvent.Set() is never called will the Background Worker simply halt and not perform any operations?

Not sure whether this is bad design by nature but supporting it unfortunately :(

EDIT some pseudo code

bool done = false;
while (!done)
{
  //some logic here
  done = System.DateTime.Now.Hour == 10;

  if (!done)
  {
    AutoResetEvent.WaitOne();
    //Question will the while loop fire again or does it wait for the Set() to be called
    //before continuing?
  }
}
도움이 되었습니까?

해결책

within DoWork() I call AutoResetEvent.WaitOne(). Does this stop the current thread until AutoResetEvent.Set() is called? If for example, AutoResetEvent.Set() is never called will the Background Worker simply halt and not perform any operations?

The answer to this is yes as cited from MSDN WaitHandle.WaitOne Method will:

Blocks the current thread until the current WaitHandle receives a signal.

EDIT: for your added code, the while loop will wait for the Set()

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top