Fixing a Cross-Thread Exception

前端 未结 1 1496
情深已故
情深已故 2021-01-22 12:59

I need some help fixing a cross-thread exception. I am using Invoke which usually solves this issue, but for some reason it is not wokring:

void paintTimer_Elaps         


        
相关标签:
1条回答
  • 2021-01-22 13:09

    Invoke will marshal back the thread that control (the one who's Invoke method is being used) was created on. Make sure the other control was also created on said thread -- that is, make sure one control wasn't created on "the wrong thread" to begin with.

    Also, since no target for Invoke was specified then it will be the this.Invoke of the containing object/class -- which might not be appropriate.

    (Or as Hasan Khan pointed out, consider the WinForms timer... the callback will always run in the thread the timer was created on, in that case.)

    Happy coding.

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