My Timer event crashes because the events are called on a different thread

前端 未结 8 1750
再見小時候
再見小時候 2021-01-21 10:49

I get the error \"Cross-thread operation not valid: Control \'label1\' accessed from a thread other than the thread it was created on.\" when I run this code:

us         


        
8条回答
  •  粉色の甜心
    2021-01-21 11:29

    I'm assuming you're talking about a WinForms application.

    When trying to update a Form element (which lives on the UI thread) from another thread, you need to use Control.Invoke or Control.BeginInvoke. You pass a delegate to the method you want to invoke (or pass an anonymous method in) and then that delegate gets called on the UI thread rather than the calling thread.

提交回复
热议问题