V
Vadym Stetsyak
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
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