WinForms thread safe control access

前端 未结 2 1896
梦毁少年i
梦毁少年i 2021-01-15 13:17

I get error Control accessed from a thread other than the thread it was created on when I try to access WinForms control. I know that all modifications of contr

2条回答
  •  没有蜡笔的小新
    2021-01-15 13:40

    You must use BeginInvoke. If you need to get a value back (eg, the Text contents of the control), you may use EndInvoke to wait for completion.

    That said, you may want to consider doing things the other way; have the GUI thread 'push' data over to background worker threads. This helps reduce the chance of racing against user input, and leads to a cleaner design with a clear separation of GUI and core logic.

提交回复
热议问题