Detecting that a ThreadPool WorkItem has completed/waiting for completion

后端 未结 2 1445
终归单人心
终归单人心 2021-01-06 08:48

For whatever reason, ThreadPool\'s QueueWorkItem doesn\'t return an IAsyncResult or some other handle to the work item, which would al

相关标签:
2条回答
  • 2021-01-06 09:11

    Well, you've got a race condition between fetching the WaitHandle and setting it. Do you really want the caller to be waiting forever if they happen to be a tiny bit late?

    You should probably do some appropriate locking and keep an "I've finished" flag so that if you do create the WaitHandle after it's finished, you set it before returning it.

    I'd also personally write a static factory method rather than just using a public constructor - or make it a "create and then explicitly start" pattern. Queuing the work item in the constructor feels weird to me.

    0 讨论(0)
  • 2021-01-06 09:15

    Why aren't you using an asynchronous delegate, as demostrated here:

    http://msdn.microsoft.com/en-us/library/h80ttd5f.aspx

    That would make Concurrent obsolete, no?

    0 讨论(0)
提交回复
热议问题