Hello, Mo!
You have to use Control.Invoke method to access your
UI.
From the docs on DataReceived
"The DataReceived event is raised on a secondary thread when data is
received from the SerialPort object. Because this event is raised on a
secondary thread, and not the main thread, attempting to modify some
elements in the main thread, such as UI elements, could raise a threading
exception. If it is necessary to modify elements in the main Form or
Control, post change requests back using Invoke, which will do the work on
the proper thread.
"
Have a look at ( http://www.codeproject.com/csharp/winformthreading.asp ) to
see how to handle mutlithreading in WinForms
You wrote on 30 Sep 2006 20:00:57 -0700:
M> Hi,
M> I have an application where I read a serial port data from a barcode
M> and set the labels on a form. I also have a textbox and button where
M> you can enter the data and here is the problem. if I use the textbox
M> and submit using thebutton everything works fine. If I use the
M> SerialDataReceivedEventHandler method of the serial port, the
M> application crahes. It seens like that when the serial port triggers
M> and I call the same routines it is not aware of the form elements and
M> hence the application hangs up. Any body has ideas on what is the
M> fix?
M> if I debug, I get the following message on when I go over the label
M> in
M> the routine
M> AutoEllipsis = Function evaluation disabled because a previous
M> function
M> evaluation timed out. You must continue execution to reenable
M> function
M> evaluation.
M> Here is the code
M> namespace ShippingLabel
M> {
M> public partial class xxxx : Form
M> {
M> SerialPort sp = new SerialPort();
M> public NDES2()
M> {
M> InitializeComponent();
M> System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
M> sp.BaudRate = 9600;
M> sp.Parity = Parity.None;
M> sp.DataBits = 8;
M> sp.StopBits = StopBits.One;
M> sp.ReadTimeout = 1500;
M> sp.DataReceived += new
M> SerialDataReceivedEventHandler(sp_DataReceived);
M> sp.Open();
M> }
M> void sp_DataReceived(object sender,
M> SerialDataReceivedEventArgs
M> e)
M> {
M> try
M> {
M> string ret = "\r";
M> string ID = sp.ReadLine().Replace(ret, "");
M> sp.Close();
M> TheNumber.Text = ID;
M> Generate_Label(ID);
M> sp.Open();
M> }
M> catch
M> {
M> ErrorLabel.Text += "Scanner Communication Failed";
M> sp.Close();
M> }
M> }
M> public void Generate_Label(string theNumber)
M> {
M> myLabel.text = theNumber;
M> }
With best regards, Vadym Stetsyak.
Blog: http://vadmyst.blogspot.com
Mo - 12 Nov 2006 19:09 GMT
Well, I am getting closer but still no Cigar!! I have been able to
delegate and invoke but now I am getting the Cross Thread error in the
method I am invoking. When I receive the serial data, I execute the
following code in the Serial_Port_Data_Received thread
SetTextValueHandler VH = new SetTextValueHandler(SetTextValue);
VH.Invoke(tempstring);
Which in turn calls the following delegate
delegate void SetTextValueHandler(string value);
void SetTextValue(string value)
{
TXNumber.Text = value;
}
and in the the line where I set the text value ( TXNumber.Text =
value;) I am getting the cross thread error. Any ideas on how to
resolve this is greatly appreciated.
Mo
Nguyễn Đan Phương - 13 Nov 2006 14:19 GMT
Add the following line to Form's constructor:
Control.CheckForIllegalCrossThreadCalls = false;
It should work. I had same problem before.
> Well, I am getting closer but still no Cigar!! I have been able to
> delegate and invoke but now I am getting the Cross Thread error in the
[quoted text clipped - 17 lines]
>
> Mo
Willy Denoyette [MVP] - 13 Nov 2006 14:56 GMT
| Add the following line to Form's constructor:
| Control.CheckForIllegalCrossThreadCalls = false;
| It should work. I had same problem before.
Bad suggestion, you are just fooling yourself by doing this, Illegal
cross-thread calls shouldn't be ignored, the property is there to help you
diagnose the problem not to ignore it.
Willy.
http://csharp.simpleserial.com