Click here to Skip to main content
Click here to Skip to main content

Cross Thread

By , 23 Oct 2006
 

Sample Image - Cross_Thread.jpg

Introduction

We can't perform operation on a control, if that control was created by another thread. While doing this kind of operations, we get Cross Thread is not valid. like that.

About this code

In this sample, i have used only one text box. I am planning to display GUID in textbox with uncondition loop. If we do that without thread, the application will be hang. so, I have created a thread in form load event.
private void Form1_Load(object sender, EventArgs e)
{
       guidThread = new Thread(new ThreadStart(CreateNewGuid));
       guidThread.Start();
}

Thread Method

There is a method CreateNewGuid() to generate guid with unconditional loop. Here, invoking a delegate event GuidThreadCallBack() to assgin the new generated guid to text box.
 public delegate void GuidThreadCallBack();
while (true)
  {
    newGUID = Guid.NewGuid().ToString();
    try
    {
     this.Invoke(new GuidThreadCallBack(setTextBoxValue));
    }
    catch { guidThread.Abort(); }
  }

Delegate Event Method

This method will assign the value to the text box. While assging the value to textbox, the delegate event method will use which thread created the text box.
private void setTextBoxValue()
 {
   textBox1.Text = newGUID;
   textBox1.Refresh();
 }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

raajaak
Software Developer General Physics Corporation (GP)
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralWatiN problem on VistamemberKvita Patil30 Jul '09 - 1:11 
M using WatiN framework for doing testing automation.
 
In XP following code giving me total open Internet explorer window.
 
WatiN.Core.IECollection objCollection =new WatiN.Core.IECollection();
 
int totCount=objCollection.Length;
 
but when i m trying to run same code on windows vista , it returns me count = 0.
 
Plaese give replay as early as . It's urgent
GeneralGot cross thread operation exceptionmemberKvita Patil3 Jul '09 - 23:55 
I m doing testing automation window application , using reflection. i.e. when i browse exe of testing application, i got all the controls ,field,method. i execute that browsed exe through my application. It allows me to read any field value but i try to change value of field it gives me following Exception "Cross Thread Exception :Control 'txtName' is accessed from a thread other than the thread it was created on".
 
i really dont understand what exactly happen.
Please give replay as early as possible.
QuestionProblem in ASP.NETmemberOm Rudi12 Aug '08 - 17:01 
How to do it, if i'm code in ASP.NET
cause in ASP.NET there is no Invoke method or something
please help...
 
Cry | :((
 
.:. Apakek System .:.

Generalanother way ...memberAntofagasta20 Jun '08 - 8:09 
This is a very short snippet that demonstrates how to deal with cross-threading (for example using text boxes and other controls) in a nice manner. Note, however, not all controls can behandled in this manner. This approach might be useful in changing various control properties like the TEXT, COLOR, VALUE, ... from a different thread.
 
public class TextBoxCallbacks
{
delegate void SetTextBoxTextColor_Callback(TextBox txtbox, Color clr);

public static void SetTextBoxTextColor(TextBox txtbox, Color clr)
{
if (txtbox != null)
{
if (txtbox.InvokeRequired)
{
SetTextBoxTextColor_Callback d =
new SetTextBoxTextColor_Callback(SetTextBoxTextColor);
txtbox.Invoke(d, new object[] { txtbox, clr });
}
else
{
if (clr != null)
{
txtbox.ForeColor = clr;
}
else
throw new Exception("TextBoxColorException!");
}
}
else
throw new Exception("TextBoxEmptyException !");
}
....
other delegates and associated methods
....
}
GeneralCross Thread not valid in Formmemberanu91417 Mar '08 - 18:24 
When i try to access a picture box in a form using another thread, I get an exception saying " Cross Thread Operation not valid in Form".
 
Please help me if anybody have an idea how to solve this.
 
Any help would be really appreciated.
 
Thank you
Questionwhat about BackgroundWorker?membereligazit24 Oct '06 - 12:03 
Why not use the BackgroundWorker (new in framework 2.0) to run long processes and update GUI?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 24 Oct 2006
Article Copyright 2006 by raajaak
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid