简体   繁体   English

IAsyncResult 的 WaitHandle 和阻塞?

[英]IAsyncResult's WaitHandle and blocking?

Let's say I have this code :假设我有这个代码:

  public class MyAsyncHandler : IHttpAsyncHandler
    {
      public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
      {
        MyAsynchOperation asynch = new MyAsynchOperation(cb, context, extraData);
        asynch.StartAsyncWork();
        return asynch;
      }
     //...

Now , if MyAsynchOperation is an IO operation , so it probably have internal methods (APM) of BeginFOO/EndFOO which are not tying a thread.现在,如果MyAsynchOperation是一个 IO 操作,那么它可能有BeginFOO/EndFOO内部方法 (APM) ,它们没有绑定线程。

When the operation is finished , a new/same thread from the thread pool (via completion ports) is dealing with the response.操作完成后,来自线程池(通过完成端口)的新线程/相同线程正在处理响应。

All ok.一切都好。

But one of the things which makes me wonder is that IAsyncResult has this property :让我想知道的一件事是IAsyncResult有这个属性:

System.Threading.WaitHandle AsyncWaitHandle { get; }

I'm familiar with WaitHadnle that it has a methods to block the current thread.我熟悉WaitHadnle ,它有一个方法来阻止当前线程。
A thread waits, or blocks , at the turnstile by calling WaitOne (for example).线程通过调用WaitOne (例如)在旋转门等待或阻塞

And so here is my questions :所以这是我的问题:

Question #1问题#1

  • Assuming MyAsynchOperation is using the APM for IO , How ( for what ) does waitHAndle is being used if there is no blocking/tying a thread ?假设MyAsynchOperation正在使用 APM 进行 IO,如果没有阻塞/绑定线程, waitHAndle如何使用的为了什么)?

Question #2问题2

  • Assuming MyAsynchOperation is NOT using the APM and it's just my implementation which internally calls new Thread().start(do some calculation) which I later later call AsyncCallback's Callback ---- does waithandle here blocks a thread ?假设MyAsynchOperation没有使用 APM,它只是我的实现,它在内部调用了new Thread().start(do some calculation) ,稍后我将调用AsyncCallback's Callback ----这里的waithandle阻塞线程吗?

#1: #1:

The entire point of WaitOne here is to allow people to write synchronous code that waits for completion;这里WaitOne的全部意义在于允许人们编写等待完成的同步代码; if you don't need to use methods like WaitOne , WaitAny etc - then don't use them ;如果您不需要使用诸如WaitOneWaitAny等方法 - 那就不要使用它们 that's fine.没关系。

#2: #2:

In both cases, whether it uses a dedicated thread, or whether it completes via some other callback, yes it blocks a thread: it blocks the thread that has requested to be blocked by calling WaitOne .这两种情况下,无论是使用专用线程,还是通过其他回调完成,是的,它都会阻塞一个线程:它通过调用WaitOne来阻塞请求阻塞的WaitOne If you don't want that: don't call WaitOne .如果你不想要:不要打电话给WaitOne


Aside: since you specify 4.0, you might find it more intuitive to return a Task<T> instead, using TaskCompletionSource<T> .旁白:由于您指定了 4.0,您可能会发现使用TaskCompletionSource<T>返回Task<T>更直观。 Edit: I realize now that this isn't an option due to satisfying the IHttpAsyncHandler.BeginProcessRequest method.编辑:我现在意识到由于满足IHttpAsyncHandler.BeginProcessRequest方法,这不是一个选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM