Help needed for 'cross-thread operation error' in C#

后端 未结 6 2018
野的像风
野的像风 2021-01-16 03:34

In the following code MessageReceived is on a different thread to label1 and when trying to access it I will get this error:

6条回答
  •  再見小時候
    2021-01-16 04:15

    In your Agent class, when you raise the MessageReceived event, you'll have to check whether the Target of the event-handler implements ISynchronizeInvoke. If it does, and if the InvokeRequired property returns true, you'll have to invoke the eventhandler.

    If you're not using WinForms, but WPF, you cannot rely anymore on ISynchronizeInvoke since the WPF controls do not implement that interface. Instead, you'll have to work with AsyncOperation and AsyncOperatoinManager.

提交回复
热议问题