.NET: How to have background thread signal main thread data is available?

后端 未结 7 1967
终归单人心
终归单人心 2021-02-14 04:54

What is the proper technique to have ThreadA signal ThreadB of some event, without having ThreadB sit blocked waiting for an e

7条回答
  •  礼貌的吻别
    2021-02-14 05:22

    You can use an AutoResetEvent (or ManualResetEvent). If you use AutoResetEvent.WaitOne(0, false), it will not block. For example:

    AutoResetEvent ev = new AutoResetEvent(false);
    ...
    if(ev.WaitOne(0, false)) {
      // event happened
    }
    else {
     // do other stuff
    }
    

提交回复
热议问题