If I understand: The first approach is to always call a call to update the interface. The second approach is if InvokeRequired returns true call invoke else - just create user interface stuff
Now, if we know that a handle has been created for management, and we only want to make small and quick updates to the user interface, we can use the first approach, and the user interface will be responsible, but with Invoke locking is still possible. If we do not want the control to be created now for the control, we must call IsHandleCreated to make sure Invoke will succeed and not throw an exception. If IsHandleCreated returns false, we cannot update through Invoke, and we must wait for the descriptor to be created.
The second approach is worse, because field.InvokeRequired can return false if the handle to control is not created, and when we call field.Text = d; A control descriptor can be created on the background thread, isolating the flow control without a message pump and making the application unstable.
So for me this is the best way:
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { input = (sender as SerialPort).ReadLine(); if (input.Contains("look for this")) { if (this.IsHandleCreated == false) {
Andriy vandych
source share