Silverlight 4线程问题

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.

Bri*_*ian 5

Silverlight需要与UI线程会合才能执行Web请求,但是在WaitOne调用中阻止了UI线程,因此您遇到了死锁.(这可以解释为Silverlight"功能".)