Click here to Skip to main content
11,701,855 members (69,749 online)
Click here to Skip to main content

Cross Thread

, 23 Oct 2006 CPOL 42.3K 793 25
Rate this:
Please Sign up or sign in to vote.
We can't perform operations on a control, if that control was created by another thread. When we try to do thatm, we will get cross thread exceptions.

Sample Image - Cross_Thread.jpg


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));

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();
     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;


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


About the Author

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

You may also be interested in...

Comments and Discussions

GeneralWatiN problem on Vista Pin
Kvita Patil30-Jul-09 1:11
memberKvita Patil30-Jul-09 1:11 
GeneralGot cross thread operation exception Pin
Kvita Patil3-Jul-09 23:55
memberKvita Patil3-Jul-09 23:55 
QuestionProblem in ASP.NET Pin
Om Rudi12-Aug-08 17:01
memberOm Rudi12-Aug-08 17:01 
Generalanother way ... Pin
Antofagasta20-Jun-08 8:09
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 });
if (clr != null)
txtbox.ForeColor = clr;
throw new Exception("TextBoxColorException!");
throw new Exception("TextBoxEmptyException !");
other delegates and associated methods
GeneralCross Thread not valid in Form Pin
anu91417-Mar-08 18:24
memberanu91417-Mar-08 18:24 
Questionwhat about BackgroundWorker? Pin
eligazit24-Oct-06 12:03
membereligazit24-Oct-06 12:03 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 24 Oct 2006
Article Copyright 2006 by raajaak
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid