【发布时间】:2013-05-23 21:47:15
【问题描述】:
我是 .Net 和 Windows Phone 开发的新手。我目前正在研究通过 HTTPWebrequest 类发出异步 Web 请求的不同方法。
异步和等待:
async 和 await 似乎是一种发出异步 Web 请求的好方法:http://blog.stephencleary.com/2012/02/async-and-await.html。我唯一担心的是,根据文档,在执行任务时调用方法线程会被挂起。我想让调用方法立即返回,怎么办?
public async Task NewStuffAsync()
{
// Use await and have fun with the new stuff.
await ...
}
public Task MyOldTaskParallelLibraryCode()
{
// Note that this is not an async method, so we can't use await in here.
...
}
public async Task ComposeAsync()
{
// We can await Tasks, regardless of where they come from.
await NewStuffAsync();
await MyOldTaskParallelLibraryCode();
}
HttpWebRequest.BeginGetResponse
这是来自 Microsoft 的示例:http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2
我想了解与回调相关的开销。
线程池
托管线程池似乎很健壮。我再次想了解与之相关的开销。
【问题讨论】:
-
如果您在使用
HttpWebRequest的情况下编译您的代码,您将收到该类已过时的警告。按照建议使用HttpClient
标签: c# asynchronous windows-phone-8 httpclient