none
A threading problem with the PictureBox.LoadAsync function RRS feed

  • Question

  • I have a problem:

    When I try to call PictureBox.LoadAsync function from a thread other than the UI thread, it won't work, the picture is not shown. I looked inside the IL code and found that the picture creates a WebRequest and calls the asynchronous functions to get the response and to read the data back. I know that I can overcome this limitation by invoking this function from the context of the UI thread  (using this.Invoke).

    My question is, why the PictureBox.LoadAsync function doesn't work when called from other threads? If that because it's like any other UI object and should be called from the UI thread, why the framework doesn't just raise a InvalidOperationException (in debug mode) that says "Cross-thread operation not valid" and doesn't let me scratching my head.

     

    Another question:

    When you try to access a UI object from another thread created by the System.Threading.Thread class and try to access a UI object, you get a InvalidOperationException (in debug only). On the other hand, when you create another working thread using System.Threading.ThreadPool.QueueUserWorkItem, you don't get that error!


    From a technology evangelist born in 1991 in Egypt, http://JustLikeAMagic.WordPress.com
    Friday, April 16, 2010 3:52 PM

Answers

  • It probably is throwing an exception on the thread where you made the call, not the UI thread. Post a code snippet.

    Can you post of sample of your 2nd issue?  Are you blocking the UI thread to synchronize threads completing their tasks?


    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, April 16, 2010 4:30 PM

All replies

  • It probably is throwing an exception on the thread where you made the call, not the UI thread. Post a code snippet.

    Can you post of sample of your 2nd issue?  Are you blocking the UI thread to synchronize threads completing their tasks?


    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, April 16, 2010 4:30 PM
  • Oh, I'm very sorry. I checked my logic again. The problem was that the controls were created in the context of another thread not the UI thread. Some sluggish code, sorry for bothering.

    I'm debugging a problematic project with much threads and less synchronization. Actually, I'm going bananas, I assert and put trace lines everywhere, I need to break it down into small pieces. May God helps.

    Thank you very much, thanks for your reply.


    From a technology evangelist born in 1991 in Egypt, http://JustLikeAMagic.WordPress.com
    Saturday, April 17, 2010 12:32 PM