Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

s
More...

Forums » .NET » .NET »

Access TextBox from a thread- winforms


Posted Date: 29 Jun 2012      Posted By:: Nagamohan kumar P     Member Level: Gold    Member Rank: 1147     Points: 5   Responses: 1



1. I am using a simple windows application in which there is a button click event.
2. There is a method to update a text box on the form which is called o=in the btn click event.
3. normal is working fine but if i use a thread and pass the method it is firing an error like this
error: Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.
code:
private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread trd = new System.Threading.Thread(doSomeOperation);
trd.Start();
MessageBox.Show("done");
}

private void doSomeOperation()
{
textBox1.Text = "";
textBox1.Refresh();
string str = @"Most of the Yandamoori Veerendranath's novels were made into commercial movies in Chiru-A.Kodanda Ramireddy-Creative Commercials banner by K.S.Rama Rao and Maestro Ilayaraja music. ";

for (int i = 0; i < 200; i++)
{
System.IO.StreamWriter file = new System.IO.StreamWriter("e:\\test.txt");
file.WriteLine(str);
file.Close();
}
}




Responses

#678167    Author: sudhajosyula      Member Level: Gold      Member Rank: 46     Date: 29/Jun/2012   Rating: 2 out of 52 out of 5     Points: 3

Hi Kumar,
This is a common error usually comes.See the below url for a detailed explanation given as how to avoid this type of errors..

http://alitarhini.wordpress.com/2011/04/06/cross-thread-
operation-not-valid-control-controlname-accessed-
from-a-thread-other-than-the-thread-it-was-created-on/



 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : how to get all registry keys & its value,data of perticular program(.exe) in vb.net?
Previous : Browse excel file and create XML map for each column
Return to Discussion Forum
Post New Message
Category:

Related Messages
Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.