cross thread issue
-
Hello I'm new at c# and I'm running into a cross thread issue. I'm reading ASCII data from the serial port using the following code below: I will like to know how to pass RxString to a different Method without getting a cross thread error? Thanks
public void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
RxString = serialPort1.ReadLine();
label1.Text = RxString;
}
catch (Exception)
{
MessageBox.Show("Data Receive Error: Close Port & Change Baud Rate" );
System.Threading.Thread.Sleep(5000);
bool dataerror = true;
DataRecError(dataerror);
return;
}
this.Invoke(new EventHandler(DisplayText));
i++;
} -
Hello I'm new at c# and I'm running into a cross thread issue. I'm reading ASCII data from the serial port using the following code below: I will like to know how to pass RxString to a different Method without getting a cross thread error? Thanks
public void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
RxString = serialPort1.ReadLine();
label1.Text = RxString;
}
catch (Exception)
{
MessageBox.Show("Data Receive Error: Close Port & Change Baud Rate" );
System.Threading.Thread.Sleep(5000);
bool dataerror = true;
DataRecError(dataerror);
return;
}
this.Invoke(new EventHandler(DisplayText));
i++;
}See: http://msdn.microsoft.com/en-us/library/ms171728(v=vs.110).aspx[^]
The difficult we do right away... ...the impossible takes slightly longer.
-
See: http://msdn.microsoft.com/en-us/library/ms171728(v=vs.110).aspx[^]
The difficult we do right away... ...the impossible takes slightly longer.
Thanks for the link, but I still cannot get it working.
-
Hello I'm new at c# and I'm running into a cross thread issue. I'm reading ASCII data from the serial port using the following code below: I will like to know how to pass RxString to a different Method without getting a cross thread error? Thanks
public void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
RxString = serialPort1.ReadLine();
label1.Text = RxString;
}
catch (Exception)
{
MessageBox.Show("Data Receive Error: Close Port & Change Baud Rate" );
System.Threading.Thread.Sleep(5000);
bool dataerror = true;
DataRecError(dataerror);
return;
}
this.Invoke(new EventHandler(DisplayText));
i++;
}The problem is the line
label1.Text = RxString;
It must not be executed in a thread different than the main thread. Try following code:
if (InvokeRequired)
{
this.Invoke(new System.Action(() => { label1.Text = RxString; }));
}
else
{
// this is the main thread anyway
label1.Text = RxString;
} -
The problem is the line
label1.Text = RxString;
It must not be executed in a thread different than the main thread. Try following code:
if (InvokeRequired)
{
this.Invoke(new System.Action(() => { label1.Text = RxString; }));
}
else
{
// this is the main thread anyway
label1.Text = RxString;
}excellent, that worked. Thank you
-
The problem is the line
label1.Text = RxString;
It must not be executed in a thread different than the main thread. Try following code:
if (InvokeRequired)
{
this.Invoke(new System.Action(() => { label1.Text = RxString; }));
}
else
{
// this is the main thread anyway
label1.Text = RxString;
} -
I think you can just always call Invoke (or BeginInvoke) for this type of thing, those calls are harmless if called in the main thread.
I am not sure. In the example above, I used a lambda which does not call the function again, and that might work. But with "normal" code like
delegate void VoidDelegate();
void SomeFunction()
{
if (InvokeRequired)
{
this.Invoke(new VoidDelegate(SomeFunction));
return;
}
// do something
}you'll run into a StackOverflow exception when omitting the InvokeRequired... Hence I always use that "safer" version, though it might not be necessary in every case.
-
I am not sure. In the example above, I used a lambda which does not call the function again, and that might work. But with "normal" code like
delegate void VoidDelegate();
void SomeFunction()
{
if (InvokeRequired)
{
this.Invoke(new VoidDelegate(SomeFunction));
return;
}
// do something
}you'll run into a StackOverflow exception when omitting the InvokeRequired... Hence I always use that "safer" version, though it might not be necessary in every case.
That's a different situation though, that's a 'make this method self-marshalling' rather than 'marshal this method call'. The StackOverflowEx is pretty obvious if you don't guard this. If you put the responsibility on the caller (i.e. they must use control.Invoke(() => control.SomeFunction())), or you provide an explicit SomeFunctionAsync (using BeginInvoke) then it's not an issue. On a different note you don't need to define a void delegate type, MethodInvoker exists for this already.