使用WebClient加载图像时Silverlight应用程序几乎冻结(ChildWindow用作ProgressBar)

时间:2012-12-28 06:39:50

标签: silverlight user-input freeze childwindow

我有这个奇怪的问题。

我主线上的代码:

WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler((ss, ee) =>
{
    Dispatcher.BeginInvoke(() =>
    {
        try
        {
            BitmapImage bi = new BitmapImage();
            bi.SetSource(ee.Result);
            WriteableBitmap wb = new WriteableBitmap(bi);
            _okAction.Invoke(linkTexBox.Text, wb);

            theProgressBarDialog.Close();
        }
        catch (Exception)
        {
            theProgressBarDialog.Close();

            string msg = "Cannot fetch the image.  Please make sure the image URL is correct";
            MessageBox.Show(msg);
        }
    });
});
client.OpenReadAsync(new Uri("Image URL without cross domain problem"));
theProgressBarDialog.Show();

图像已成功加载,并在我的画布上呈现。

奇怪的行为有时候,我的整个Silverlight应用程序看起来都很冷,应用程序不会响应任何用户操作,除非右键单击,弹出默认的Silverlight上下文菜单。

强制禁用按钮。

调试时,不会抛出任何异常。

编辑:如果我设置client.AllowReadStreamBuffering = false,我将获得例外:

{System.NotSupportedException: Read is not supported on the main thread when buffering is disabled.
   at MS.Internal.InternalNetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.Windows.Media.Imaging.BitmapSource.ReadStream(Stream streamSource, Byte[]& buffer, Int32& position)
   at System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(Stream streamSource)
   at System.Windows.Media.Imaging.BitmapImage.SetSourceInternal(Stream streamSource)
   at System.Windows.Media.Imaging.BitmapSource.SetSource(Stream streamSource)
   at ToonGui.ImportImageDialog.<>c__DisplayClass2.<OKButton_Click>b__1()}
    [System.NotSupportedException]: {System.NotSupportedException: Read is not supported on the main thread when buffering is disabled.
   at MS.Internal.InternalNetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.Windows.Media.Imaging.BitmapSource.ReadStream(Stream streamSource, Byte[]& buffer, Int32& position)
   at System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(Stream streamSource)
   at System.Windows.Media.Imaging.BitmapImage.SetSourceInternal(Stream streamSource)
   at System.Windows.Media.Imaging.BitmapSource.SetSource(Stream streamSource)
   at ToonGui.ImportImageDialog.<>c__DisplayClass2.<OKButton_Click>b__1()}
    Data: {System.Collections.ListDictionaryInternal}
    InnerException: null
    Message: "Read is not supported on the main thread when buffering is disabled."
    StackTrace: "   at MS.Internal.InternalNetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count)\r\n   at System.Windows.Media.Imaging.BitmapSource.ReadStream(Stream streamSource, Byte[]& buffer, Int32& position)\r\n   at System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(Stream streamSource)\r\n   at System.Windows.Media.Imaging.BitmapImage.SetSourceInternal(Stream streamSource)\r\n   at System.Windows.Media.Imaging.BitmapSource.SetSource(Stream streamSource)\r\n   at ToonGui.ImportImageDialog.<>c__DisplayClass2.<OKButton_Click>b__1()"

我是否必须使用BackgroundWorker类让下载任务在另一个线程中运行?

1 个答案:

答案 0 :(得分:1)

谢谢你们,我终于找到了答案。这是一个Siverlight错误,这是微软的错。

正如我的一位朋友所说微软。。。总是有一堆令人想杀人的问题(意思是Microsoft.... always has some issues that drive you crazy enough so that you want to shoot someone

原因来自theProgressBarDialog,这是一个ChildWindow,这是一个愚蠢的错误:关闭后,有时会让父窗口(主应用程序)被禁用。

  1. Silverlight:Modal ChildWindow在关闭后保持父灰色: (也可以尝试调用this.DialogResult = true而不是Close方法。) Silverlight: Modal ChildWindow keeps parent grayed after closing

  2. Silverlight 4 ChildWindow在关闭后将父级禁用: http://social.msdn.microsoft.com/Forums/en-US/silverlightbugs/thread/56d0dc0b-3711-4643-b56f-2c94344e3d3a/

  3. 解决方案是:

            private void ChildWindow_Closed(object sender, System.EventArgs e)
            {
    // TODO: should we use apply this fix to all ChildWindow? (make a TnChildWindow)
    //   1. Silverlight: Modal ChildWindow keeps parent grayed after closing:
    //      (Also try calling this.DialogResult = true instead of the Close method.)
    //      https://stackoverflow.com/questions/6456952/silverlight-modal-childwindow-keeps-parent-grayed-after-closing
    //   2. Silverlight 4 ChildWindow leaves parent disabled after closing:
    //      http://social.msdn.microsoft.com/Forums/en-US/silverlightbugs/thread/56d0dc0b-3711-4643-b56f-2c94344e3d3a/
                Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, true);
            }