473,320 Members | 2,035 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,320 software developers and data experts.

Accessing Textboxes from threads.

Im a beginner with C#, but as I try to access a textbox from a thread I get
the error message :

"An unhandled exception of type 'System.InvalidOperationException' occurred
in System.Windows.Forms.dll

Additional information: Cross-thread operation not valid: Control
'textBoxStatus' accessed from a thread other than the thread it was created
on."

How do I get around this, as the thread reads incomming data from a socket
and I want to write it down as soon as I get it....??
Apr 3 '06 #1
3 29634
Hi Vadym.
I always get amazed about how fast I do get replys from you guys.
Thanx a lot, I now got it to work... =)

/Arv

"Vadym Stetsyak" wrote:
Hello, Arv!

AC> "An unhandled exception of type 'System.InvalidOperationException'
AC> occurred in System.Windows.Forms.dll

AC> Additional information: Cross-thread operation not valid: Control
AC> 'textBoxStatus' accessed from a thread other than the thread it was
AC> created on."

To use winforms control from separate thread you should call Control.Invoke method. In your case this will be
TextBox.Invoke

The code may look like this:

delegate void OutputUpdateDelegate(string data);

public void UpdateTextBox(string data)
{
if ( txtOutput.InvokeRequired )
txtOutput.Invoke(new OutputUpdateDelegate(OutputUpdateCallback),
new object[] { data });
else
OutputUpdateCallback(data); //call directly
}

private void OutputUpdateCallback(string data)
{
txtOutput.Text += data;
}

UpdateTextBox method can be called from other threads
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot

Apr 3 '06 #2
Hello Arv,

That's why we are here.

If you use .NET 2.0 there is more elegant way for doing this
read there http://spaces.msn.com/staceyw/blog/cns!F4A38E96E598161E!652.entry
AC> Hi Vadym.
AC> I always get amazed about how fast I do get replys from you guys.
AC> Thanx a lot, I now got it to work... =)
AC> /Arv
AC>
AC> "Vadym Stetsyak" wrote:
AC>
Hello, Arv!

AC> "An unhandled exception of type
'System.InvalidOperationException' AC> occurred in
System.Windows.Forms.dll

AC> Additional information: Cross-thread operation not valid: Control
AC> 'textBoxStatus' accessed from a thread other than the thread it
was AC> created on."

To use winforms control from separate thread you should call
Control.Invoke method. In your case this will be TextBox.Invoke

The code may look like this:

delegate void OutputUpdateDelegate(string data);

public void UpdateTextBox(string data)
{
if ( txtOutput.InvokeRequired )
txtOutput.Invoke(new OutputUpdateDelegate(OutputUpdateCallback),
new object[] { data });
else
OutputUpdateCallback(data); //call directly
}
private void OutputUpdateCallback(string data)
{
txtOutput.Text += data;
}
UpdateTextBox method can be called from other threads
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Apr 3 '06 #3
Thanks Michael. Using BeginInvoke as I showed in the blog could have some slight side effects depending on what you expect. I might update that sample like so using blocking Invoke instead:

private void button2_Click(object sender, EventArgs e)

{

this.button2.Enabled = false;

// Worker Thread.

new Thread(delegate()

{

for (int i = 0; i < 100; Interlocked.Increment(ref i))

{

// Update UI on UI thread using blocking Invoke.

this.Invoke((ThreadStart)delegate()

{

this.progressBar1.Value = i;

this.textBox1.Text = "Percent Complete: " + i + "%";

});

Thread.Sleep(5); // Just add a pause to sim work.

}

// Call any completion code here. All Invokes above must have completed as Invoke is blocking.

this.Invoke((ThreadStart)delegate()

{

this.textBox1.Text = "Percent Complete: 100%";

this.button2.Enabled = true;

});

}).Start();

}
--
William Stacey [MVP]

"Michael Nemtsev" <ne*****@msn.com> wrote in message news:9c**************************@msnews.microsoft .com...
| Hello Arv,
|
| That's why we are here.
|
| If you use .NET 2.0 there is more elegant way for doing this
| read there http://spaces.msn.com/staceyw/blog/cns!F4A38E96E598161E!652.entry
|
|
| AC> Hi Vadym.
| AC> I always get amazed about how fast I do get replys from you guys.
| AC> Thanx a lot, I now got it to work... =)
| AC> /Arv
| AC>
| AC> "Vadym Stetsyak" wrote:
| AC>
| >> Hello, Arv!
| >>
| >> AC> "An unhandled exception of type
| >> 'System.InvalidOperationException' AC> occurred in
| >> System.Windows.Forms.dll
| >>
| >> AC> Additional information: Cross-thread operation not valid: Control
| >> AC> 'textBoxStatus' accessed from a thread other than the thread it
| >> was AC> created on."
| >>
| >> To use winforms control from separate thread you should call
| >> Control.Invoke method. In your case this will be TextBox.Invoke
| >>
| >> The code may look like this:
| >>
| >> delegate void OutputUpdateDelegate(string data);
| >>
| >> public void UpdateTextBox(string data)
| >> {
| >> if ( txtOutput.InvokeRequired )
| >> txtOutput.Invoke(new OutputUpdateDelegate(OutputUpdateCallback),
| >> new object[] { data });
| >> else
| >> OutputUpdateCallback(data); //call directly
| >> }
| >> private void OutputUpdateCallback(string data)
| >> {
| >> txtOutput.Text += data;
| >> }
| >> UpdateTextBox method can be called from other threads
| >> --
| >> Regards, Vadym Stetsyak
| >> www: http://vadmyst.blogspot
| ---
| WBR,
| Michael Nemtsev :: blog: http://spaces.msn.com/laflour
|
| "At times one remains faithful to a cause only because its opponents do not
| cease to be insipid." (c) Friedrich Nietzsche
|
|
Apr 3 '06 #4

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

Similar topics

1
by: Wesman | last post by:
Threads, textboxes and scrolling Thanks in advance for any information on this matter. I have run into a small richtextbox, scrolling and tread issue. Which has me totally confused. Instead of...
2
by: James Zhuo | last post by:
Accessing Items in a Collection works with the as suggested by Kevin and Chris (thx) But when i tried to use the same technic with databinding within the aspx file, it doesn't seem to work. This...
3
by: Alex | last post by:
I'm having a problem porting an ASP solution to ASPX. In the ASP solution I'm accessing a DCOM server, create sub DCOM objects and call functions from VB script on the ASP pages. The DCOM object...
9
by: Bob Day | last post by:
VS 2003, vb.net , sql msde... I have an application with multiple threads running. Its a telephony application where each thread represents a telephone line. For code that would be the same...
2
by: Robert Kemp | last post by:
I am creating a non-visible/tray control that monitors other custom controls for changes (among other things). One of the monitored controls is a new textbox that adds functionality to the...
2
by: Jon C | last post by:
Hi, I've written a multi-threaded web spider which stores search results in a Shared ArrayList. Since each thread can launch further threads, the problem I have is pausing my main sub until all...
1
by: amurra06 | last post by:
I have a form that contains a tabpage and in that tabpage is a usercontrol that conatins a bunch of textboxes. I have arranged these textboxes in groupboxes to organize them better. What I do is to...
5
by: Hyun-jik Bae | last post by:
I got a curiosity while using CLR. How can it be done that accessing object data by CLR application is safe even while garbage collector is working? Is the GC thread running in realtime priority...
1
by: Peter | last post by:
Hi, I have a Dictionary<key, valuewhich is accessed by three threads. One thread puts my value objects in the dictionary (occasionally), and also updates the contents of existing value objects -...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.