Is it evil to update a pictureBox from a background C# thread?

前端 未结 3 763
失恋的感觉
失恋的感觉 2020-12-12 02:16

First of all, the code below seems to be working. It extracts jpeg images from a continuous byte stream and displays them in a pictureBox as they arrive if the encapsulating

相关标签:
3条回答
  • 2020-12-12 02:59

    I think all access to UI controls should be done from UI thread. Modifying control from the thread that doesn't own the underlying handle may have undesirable effects. In the best case scenario the exception will be thrown, in the worst case everything may seem to be all right until some race condition happens (and you may spend lots of time trying to replicate it).

    Use Invoke method, passing your delegate that will be executed on the UI thread.

    0 讨论(0)
  • 2020-12-12 03:08

    Are you sure that even works at all? I don't see why it wouldn't raise a InvalidOperationException: (Cross-thread operation not valid) as the control is being updated from a thread other than the one which is was created on. You should update the UI via a delegate method that is invoked on the UI thread.

    0 讨论(0)
  • 2020-12-12 03:12

    Why you don't use Invoke to update the PictureBox?

    0 讨论(0)
提交回复
热议问题