as-*_*cii 1 c# silverlight events multithreading
最近我一直在Silverlight中开发一个使用上传的应用程序.
我使用WebClientclass与HttpHandler我服务器中的一个进行通信.提供的方法WebClient是异步的并且使用基于事件的APM:考虑到Silverlight的范围是具有流畅的UI,不会阻止我想要的,有趣的是,尝试使调用同步.
AutoResetEvent _uploadedEvent = new AutoResetEvent(false);
foreach (var item in _fileInfos)
{
WebClient client = new WebClient();
client.OpenWriteCompleted += (sender, e) =>
{
try
{
using (FileStream fs = item.OpenRead())
using (Stream stream = y.Result)
{
while (true)
{
byte[] buffer = new byte[8192];
int readBytes = fs.Read(buffer, 0, buffer.Length);
if (readBytes == 0)
break;
stream.Write(buffer, 0, readBytes);
}
}
}
finally
{
_uploadedEvent.Set();
}
};
client.OpenWriteAsync(new Uri(_receiverUri));
_uploadedEvent.WaitOne();
}
Run Code Online (Sandbox Code Playgroud)
这种使调用同步的方法在Silverlight中不起作用,但它在WPF中起作用.现在我注意到我不是唯一一个遇到这个问题的人:https://stackoverflow.com/questions/3819650/silverlight-httprequest-thread-problem
你认为这是什么问题?
提前致谢.
AS-CII.