Does AsyncWaitHandle.WaitOne block the CLR thread?

2019-05-11 09:52发布

问题:

I have that question, does AsyncWaitHandle.WaitOne block the CLR thread? or does it create an I/O completion port?

For example, when I run my application I launch a task 'A' that initializes some data, when new requests arrives, I want them to wait till 'A' has finished, so I can do a IAsyncResult.AsyncWaitHandle.WaitOne, but... does it block the calling thread till 'A' ends or does it create a I/O completion port that will be also notified when 'A' finish.

If not, is there a way to do that?

Regards.

回答1:

Yes, it blocks the thread, but like any other WaitHandle, it blocks in the OS kernel so it doesn't take any cpu time.

If you don't want to block your thread, but do want a "callback", you can use the thread pool:

ThreadPool.RegisterWaitForSingleObject( waitHandle, callback, ...