473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Threading in System.IO.Ports

I downloaded the Visual C# express, which includes the .NET Framework 2.0.

The following exception is generated:

{"Illegal cross-thread operation: Control 'textBox1' accessed from a
thread other than the thread it was created on."}

on the following code:

namespace WindowsApplication1
{
partial class Form1 : Form
{
//private string buffer;
public Form1()
{
InitializeComponent();
this.serialPort1.Open();
if (this.serialPort1.IsOpen)
this.textBox1.AppendText("Port opened successfully");
}

private void serialPort1_ReceivedEvent(object sender,
System.IO.Ports.SerialReceivedEventArgs e)
{
this.textBox1.AppendText(this.serialPort1.ReadExis ting()); ;
}
}
}

Both SerialPort and TextBox object are dragged from the Toolbox in
Visual C# 2005 Express Beta.

Alex
Nov 16 '05 #1
1 3995
The ReceivedEvent is probably raised from another thread. The UI controls
are not thread safe. Previous versions of .Net would allow these
thread-unsafe to be made, but they will cause weird things to happen (often
resulting in the application just disappearing) eventually. .Net 2 detects
these bad cross-thread calls, and raises an exception.

When in another thread, you should invoke all UI control methods using
BeginInvoke and EndInvoke rather than directly.

See this for more info:
http://msdn.microsoft.com/library/en...asp?frame=true

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"Alex Moskalyuk" <usenet/puttheathere/moskalyuk.com> wrote in message
news:u7****************@TK2MSFTNGP11.phx.gbl...
I downloaded the Visual C# express, which includes the .NET Framework 2.0.

The following exception is generated:

{"Illegal cross-thread operation: Control 'textBox1' accessed from a
thread other than the thread it was created on."}

on the following code:

namespace WindowsApplication1
{
partial class Form1 : Form
{
//private string buffer;
public Form1()
{
InitializeComponent();
this.serialPort1.Open();
if (this.serialPort1.IsOpen)
this.textBox1.AppendText("Port opened successfully");
}

private void serialPort1_ReceivedEvent(object sender,
System.IO.Ports.SerialReceivedEventArgs e)
{
this.textBox1.AppendText(this.serialPort1.ReadExis ting()); ;
}
}
}

Both SerialPort and TextBox object are dragged from the Toolbox in
Visual C# 2005 Express Beta.

Alex

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: willie | last post by:
Hi, I'm writing a program which requires the use of three serial ports and one parallel port. My application has a scanning devices on each port, which I can access fine with pyserial. ...
0
by: Helge Jensen | last post by:
Having posted in microsoft.public.dotnet.framework.sdk and microsoft.public.dotnet.framework.wmi without receiving any response, I posthere on the off-chance that someone who isn't following those...
7
by: Terry Olsen | last post by:
I run this code: Private Sub p_recv(ByVal sender As Object, ByVal e As SerialReceivedEventArgs) Handles p.ReceivedEvent txtRecv.Text += p.ReadExisting End Sub I get this error: ...
13
by: notregister | last post by:
my program have as many as 8 threads running at a same time. can different threads open a same file at the same time? let say thread1 open a file abc.txt at the same time with thread 2, both doing...
1
by: Gary Kahrau | last post by:
I hope this is a simple question for threading people. VB 2005 Beta2 has a new SerialPort control. I can read the data that I want just fine. However when I try to display the text, I get a...
5
by: LongBow | last post by:
Hello, Is there a way, in .NET, to determine what are the avialable Serial (Communications) Ports on a Windows OS and is there a way to determine that port isn't being use other than attempting...
1
by: Pooh | last post by:
hi all, i faced an error in my project: "An unhandled exception of type 'System.Threading.ThreadStateException' occurred in system.windows.forms.dll Additional information: Could not instantiate...
6
by: tubby | last post by:
I have a program written in Python that checks a class B network (65536 hosts) for web servers. It does a simple TCP socket connect to port 80 and times out after a certain periods of time. The...
1
by: JDS | last post by:
I am getting the following error in my application: System.UnauthorizedAccessException was unhandled Message="Access to the port is denied." Source="System" StackTrace: at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.