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

前端 未结 8 1672
再見小時候
再見小時候 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条回答
  •  梦毁少年i
    2021-01-21 11:30

    We have 3 Timer classes in NET (Timers.Timer, Threading.Timer and Windows.Forms.Timer) but only the Windows.Forms one has a Tick event.

    When used normally (ie dragged to the Form in Design-Time or created in some Form Code) the event is running on the Main thread and your problem should not occur.

    It is therefore most likely that you create the Timer object in another thread, you should probably Edit your question to show us how/where you create it and tell us if it is on another Thread on purpose.

提交回复
热议问题