I’m doing async network in C#.NET with the TcpClient and TcpListener classes.
I use WinForms for the GUI.
Whenever I receive data from a remote computer, the operation is done on a different underlying thread.
What I need to do is to update the GUI of my application whenever I receive a network response.
// this method is called whenever data is received
// it's async so it runs on a different thread
private void OnRead(IAsyncResult result)
{
// update the GUI here, which runs on the main thread
// (a direct modification of the GUI would throw a cross-thread GUI exception)
}
How can I achieve that?
In Winforms you need to use Control.Invoke Method (Delegate) to make sure that control is updated in the UI thread.
Example:
Usage: