473,378 Members | 1,436 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,378 software developers and data experts.

cannot display thread result on a textbox

2
Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on

i usually get this error,,i want to display the thread result to a textbox but i just can't..please help me,,thanks a lot

here's my code:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10.  
  11. namespace MultiThread
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         Thread thread;
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         // delegate is used to communicate with UI Thread from a non-UI thread
  22.         //public delegate void UpdateTextCallback(string message);
  23.  
  24.         private void Form1_Load(object sender, EventArgs e)
  25.         {
  26.  
  27.         }
  28.  
  29.         private void button1_Click(object sender, EventArgs e)
  30.         {
  31.             // Creates a new thread
  32.             thread = new Thread(new ThreadStart(TestThread));
  33.             thread.IsBackground = true;
  34.             thread.Start();
  35.  
  36.         }
  37.  
  38.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  39.         {
  40.             thread.Abort();
  41.         }
  42.  
  43.         private void TestThread()
  44.         {
  45.             for (int i = 0; i <= 1000; i++)
  46.             {
  47.                 //Thread.Sleep(1000);
  48.                 textBox1.Text = textBox1.Text + "Thread";
  49.                 //textBox1.BeginInvoke(new UpdateTextCallback(UpdateText), new object[] { i.ToString() });
  50.                 Thread.Sleep(100);
  51.             }
  52.         }
  53.  
  54.         private void UpdateText(string message)
  55.         {
  56.             textBox1.Text = message;
  57.         }
  58.     }
  59. }
Dec 7 '10 #1
1 2085
Raj K
9
Use this code to update the text box

this.BeginInvoke(new Action(delegate() {textBox1.Text = textBox1.Text + "Thread";}));
Dec 8 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Julien | last post by:
When trying to run this code, I get the following error message : Unhandled Exception: System.Runtime.InteropServices.COMException (0x80010106): Cannot change thread mode after it is set. 1....
2
by: Mamatha | last post by:
Hi I want to add an icon to the textbox's text. I don't know how to display icon in textbox in VB.NET. If any one knows please let me know. Thanks in advance. Mamatha
5
by: degnau | last post by:
I have an Order form. When I change the quantity I go to a script to calculate anddisplay the Amount = UnitPrice * Quantity But the Amount only display if I click somewhere else as the...
0
by: Sharmila06 | last post by:
hi Am a dotnet student working in a C# project. i want to search n display the result in datagrid , i want to search with the starting character once i type the first char in textbox i want the...
3
by: remya1000 | last post by:
i'm using ASP with MSAccess as database. i have two buttons and two textbox in my page. when i press my first button (First month) i need to display the current month in one textbox and last one...
1
by: Claudia Fong | last post by:
Hi, In my form I have a textbox. I have a class where I have a method to add two number. Can I define in the method of my class to display the result in the textbox of my form? I the...
0
tjc0ol
by: tjc0ol | last post by:
Hi guys, I'm a newbie of this stuffs, We had a small office network (1 Windows 2K - Server) and (3 Windows XP - Client). I am testing to 1 PC (Windows 2K) installed with Licensed Wingate...
4
by: eschneider | last post by:
I get the following error when trying to browse the .asmx. I get the same thing when trying to add a reference. using .NET 2.0 There is no error message. Any ideas? Thanks,
1
tharini
by: tharini | last post by:
I have five textboxes.I need to add the values in all the five textboxes and display the result in the sixth textbox without doing any submit action . if i change the values in these five textboxes...
1
by: Louloul | last post by:
Hello I am working with a wamp program. when I load the page, it displays this sentence " Internet Explorer cannot display the webpage ". Here is the code <?php session_start(); ?>...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.