1

I have this weird issue.

The code I have on my main thread:

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();

The image is loaded successfully, and got rendered on my canvas.

The weird behavior is sometimes, my whole Silverlight application seems freezing, the app does not respond to any user action EXCEPT the right-click, which pops up the default Silverlight context menu.

The buttons are disabled forcefully.

When debugging, no exception is thrown.

EDIT: If I set client.AllowReadStreamBuffering = false, I will get exception:

{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()"

Do I have to use a BackgroundWorker class to let the downloading task run in another thread?

Peter Lee
  • 12,931
  • 11
  • 73
  • 100
  • Do you have AllowReadStreamBuffering set to false? – Aleksandr Dubinsky Dec 29 '12 at 12:31
  • If I `AllowReadStreamBuffering = false`, exception thrown as I edited. Do I have to use a `BackgroundWorker` class to let the downloading task run in another thread? – Peter Lee Jan 04 '13 at 07:03
  • No, don't use `AllowReadStreamBuffering = false` it is buggy. What do you do with the writeable bitmap? Does this happen when you run it add debugging? You can pause it top see the stack trace. Even better is to launch profiling. – Aleksandr Dubinsky Jan 04 '13 at 13:15
  • I used the WriteableBitmap to load the image from internet and then save the image data to my data structure memory. This freezing happens quite often. I cannot do stack trace, because there is no exception thrown, and the right-click context menu is till there. – Peter Lee Jan 04 '13 at 18:38
  • What do you do with the image data? Are you sure you need WriteableBitmap or are using it properly? It can be a slow class. You can get the stack trace whenever you "pause" execution in Visual Studio (just open the stack trace window). If your edition of VS doesn't support profiling, download a free trial of Ultimate or just find the key online. – Aleksandr Dubinsky Jan 05 '13 at 16:03

1 Answers1

1

Thank you guys, I finally found the answer. It's a Siverlight bug, which is Microsoft's fault.

As one of my friends said 微软。。。总是有一堆令人想杀人的问题 (which means Microsoft.... always has some issues that drive you crazy enough so that you want to shoot someone)

The cause is from the theProgressBarDialog which is a ChildWindow, which haa a stupid bug: after closing, sometime it leaves the parent window (the main app) disabled.

  1. Silverlight: Modal ChildWindow keeps parent grayed after closing: (Also try calling this.DialogResult = true instead of the Close method.) 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/

The solution is:

        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);
        }
Community
  • 1
  • 1
Peter Lee
  • 12,931
  • 11
  • 73
  • 100
  • Your friend is right. I have a love-hate relationship with every MS tech. Silverlights is especially buggy, which sucks because those bugs will never be fixed. I got into java programming using Netbeans. It's very similar, except all the little things don't make you want to tear your hair out. To bad javafx is taking so long, but should become a great substitute for silverlight. – Aleksandr Dubinsky Jan 06 '13 at 14:17
  • I ran into a similar bug with childwindow too (won't open a second time). sorry i didn't mention it. it threw me of when you said "freezes". i assumed the cpu was going to 100% on the ui thread, which is what a silverlight freeze usually means. you should have said the ui is all grayed out and disabled. – Aleksandr Dubinsky Jan 06 '13 at 14:21
  • I did mention `disabled` in `The weird behavior is sometimes, my whole Silverlight application seems freezing, the app does not respond to any user action EXCEPT the right-click, which pops up the default Silverlight context menu. The buttons are disabled forcefully.` – Peter Lee Jan 06 '13 at 18:18