I am a medical physicist new to the .Net Framework and am trying to write a
simple app that will monitor the output from a serial port on one of my
linear accelerators using the new Serialport object and write the output to a
textBox in my application for viewing. I however keep getting this error and
am unable to figure out how to get around this.
System.InvalidO perationExcepti on was unhandled
Message="Cross-thread operation not valid: Control 'textBox1' accessed
from a thread other than the thread it was created on."
Source="System. Windows.Forms"
I have attached the code from my simple form for review in the event some
one can help me
Thank You
Michael Tallhamer
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.IO;
using System.IO.Ports ;
namespace MorningCheckRea der
{
public partial class Form1 : Form
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM 1", 9600, Parity.None,
8,
StopBits.One);
public Form1()
{
InitializeCompo nent();
}
private void button1_Click(o bject sender, EventArgs e)
{
// Attach a method to be called when there is data waiting in
the port's
buffer
port.DataReceiv ed += new SerialDataRecei vedEventHandler
(port_DataRecei ved);
//Open the port
port.Open();
}
private void button2_Click(o bject sender, EventArgs e)
{
port.Close();
}
private void port_DataReceiv ed(object sender,
SerialDataRecei vedEventArgs e)
{
// Show all the incoming data in the port's buffer
if (textBox1.Text. Length > 0)
{
textBox1.Text = textBox1.Text.I nsert(textBox1. Text.Length -
1,
port.ReadExisti ng());
}
else
{
textBox1.Text = port.ReadExisti ng();
}
}
}
}
--
Mike
simple app that will monitor the output from a serial port on one of my
linear accelerators using the new Serialport object and write the output to a
textBox in my application for viewing. I however keep getting this error and
am unable to figure out how to get around this.
System.InvalidO perationExcepti on was unhandled
Message="Cross-thread operation not valid: Control 'textBox1' accessed
from a thread other than the thread it was created on."
Source="System. Windows.Forms"
I have attached the code from my simple form for review in the event some
one can help me
Thank You
Michael Tallhamer
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.IO;
using System.IO.Ports ;
namespace MorningCheckRea der
{
public partial class Form1 : Form
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM 1", 9600, Parity.None,
8,
StopBits.One);
public Form1()
{
InitializeCompo nent();
}
private void button1_Click(o bject sender, EventArgs e)
{
// Attach a method to be called when there is data waiting in
the port's
buffer
port.DataReceiv ed += new SerialDataRecei vedEventHandler
(port_DataRecei ved);
//Open the port
port.Open();
}
private void button2_Click(o bject sender, EventArgs e)
{
port.Close();
}
private void port_DataReceiv ed(object sender,
SerialDataRecei vedEventArgs e)
{
// Show all the incoming data in the port's buffer
if (textBox1.Text. Length > 0)
{
textBox1.Text = textBox1.Text.I nsert(textBox1. Text.Length -
1,
port.ReadExisti ng());
}
else
{
textBox1.Text = port.ReadExisti ng();
}
}
}
}
--
Mike
Comment