G
Guest
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.InvalidOperationException 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
namespace MorningCheckReader
{
public partial class Form1 : Form
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM1", 9600, Parity.None,
8,
StopBits.One);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Attach a method to be called when there is data waiting in
the port's
buffer
port.DataReceived += new SerialDataReceivedEventHandler
(port_DataReceived);
//Open the port
port.Open();
}
private void button2_Click(object sender, EventArgs e)
{
port.Close();
}
private void port_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
// Show all the incoming data in the port's buffer
if (textBox1.Text.Length > 0)
{
textBox1.Text = textBox1.Text.Insert(textBox1.Text.Length -
1,
port.ReadExisting());
}
else
{
textBox1.Text = port.ReadExisting();
}
}
}
}
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.InvalidOperationException 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
namespace MorningCheckReader
{
public partial class Form1 : Form
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM1", 9600, Parity.None,
8,
StopBits.One);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Attach a method to be called when there is data waiting in
the port's
buffer
port.DataReceived += new SerialDataReceivedEventHandler
(port_DataReceived);
//Open the port
port.Open();
}
private void button2_Click(object sender, EventArgs e)
{
port.Close();
}
private void port_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
// Show all the incoming data in the port's buffer
if (textBox1.Text.Length > 0)
{
textBox1.Text = textBox1.Text.Insert(textBox1.Text.Length -
1,
port.ReadExisting());
}
else
{
textBox1.Text = port.ReadExisting();
}
}
}
}