我有一个 Silverlight 应用程序,其中我有多个请求发送到服务器。我希望所有这些请求都同步,即通过队列或通过多任务处理。这样做的最佳方法是什么。请提供一些示例在 io 可以这样做的 Silverlight 中。
最佳答案
看看 ASYNC CTP 框架:
http://msdn.microsoft.com/en-us/vstudio/gg316360
它提供了处理/同步异步请求的简单方法,例如以下示例:
public async void AsyncWebRequestGetResponse()
{
var response = await WebRequest.Create("http://www.weather.gov").GetResponseAsync();
var stream = response.GetResponseStream();
Console.WriteLine("First byte: {0}", stream.ReadByte().ToString("X2"));
}
或者
public async void AsyncForEach()
{
var uris = new List<Uri> { new Uri("http://www.weather.gov"), new Uri("http://www.weather.gov/climate/"), new Uri("http://www.weather.gov/rss/") };
foreach (var uri in uris)
{
WriteLinePageTitle(await new WebClient().DownloadStringTaskAsync(uri));
}
}
经典的方法是像 AutoResetEvent 一样使用 WaitHandles 来同步请求。
关于asp.net - Silverlight 中的多任务和多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2468923/