Missing implementation of AsyncWaitHandle property on System.Threading.Tasks.Task class

Answered Missing implementation of AsyncWaitHandle property on System.Threading.Tasks.Task class

  • Thursday, September 06, 2012 3:43 PM
     
     

    Hi,

    I'm just really curious about this one. How is it possible that System.Threading.Tasks.Task implements the System.IAsyncResult interface, but the System.IAsyncResult.AsyncWaitHandle property implementation is missing? I thought when you implement an interface, all members of the interface becomes "public" in the implementing class. I just want to know why. Thanks.

All Replies

  • Thursday, September 06, 2012 4:10 PM
    Moderator
     
     

    It's there - scroll to the bottom of the Task documentation, and you'll see that it implements IAsyncResult.AsyncWaitHandle via an explicit interface implementation.

    The type is there, but it's not "visible" on Task unless you cast Task to an IAsyncResult.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • Friday, September 07, 2012 1:57 AM
     
      Has Code

    Hi Reed,

    Thanks for the reply.

    Yes, it's in the documentation, but when I go to my code and "go to definition" of System.Threading.Tasks.Task, the AsyncWaitHandle member doesn't show up.

    And yes, if I cast System.Threading.Tasks.Task to System.IAsyncResult, AsyncWaitHandle shows up, but when I create a System.Threading.Tasks.Task like this:

    Task t1 = Task.Factory.StartNew(() => Console.WriteLine("Hello"));
    
    // ts.AsyncWaitHandle.WaitOne() // can't do this
    AsyncWaitHandle is not available. How's .NET/Visual Studio doing that? Is this a special case? Can I also hide implementations of my own classes that implements interfaces?

    Thanks!

  • Friday, September 07, 2012 2:21 AM
    Moderator
     
     Answered Has Code

    It's using an explicit implementation.  You ahve to do:

    (t1 as IAsyncResult).AsyncWaitHandle.WaitOne();

    See the link above for explicit interface implemetnation for details.

    (That being said, you can also just do :  t1.Wait() )


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked As Answer by ponki.d.monkey Friday, September 07, 2012 6:17 AM
    •