Is there any way to await an IAsyncResult in Windows Phone 8?

后端 未结 2 1212
野趣味
野趣味 2021-01-12 12:48

How can I use await with HttpWebRequest in Windows Phone 8 ?

Is there a way to make the IAsyncResult stuff work with await?

private          


        
相关标签:
2条回答
  • 2021-01-12 13:10

    If you install the Microsoft.Bcl.Async package, you get a few async-ready extension methods, including ones for HttpWebRequest.

    0 讨论(0)
  • 2021-01-12 13:24

    Use TaskFactory.FromAsync to create a Task<Stream> from the BeginGetRequestStream/EndGetRequestStream methods. Then you can get rid of your OnGotWebRequest completely, and do the same thing for the response.

    Note that currently you're calling EndGetResponse when a BeginGetRequestStream call completes, which is inappropriate to start with - you've got to call the EndFoo method to match the BeginFoo you originally called. Did you mean to call BeginGetResponse?

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