Click here to Skip to main content
15,886,919 members
Articles / Web Development / ASP.NET
Article

Cross Thread

Rate me:
Please Sign up or sign in to vote.
3.00/5 (6 votes)
23 Oct 2006CPOL 55.4K   950   25   6
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

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)


Written By
Software Developer General Physics Corporation (GP)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralWatiN problem on Vista Pin
Kvita Patil30-Jul-09 1:11
Kvita Patil30-Jul-09 1:11 
GeneralGot cross thread operation exception Pin
Kvita Patil3-Jul-09 23:55
Kvita 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.NET Pin
Om Rudi12-Aug-08 17:01
Om Rudi12-Aug-08 17:01 
Generalanother way ... Pin
Antofagasta20-Jun-08 8:09
Antofagasta20-Jun-08 8:09 
GeneralCross Thread not valid in Form Pin
anu91417-Mar-08 18:24
anu91417-Mar-08 18:24 
Questionwhat about BackgroundWorker? Pin
eligazit24-Oct-06 12:03
eligazit24-Oct-06 12:03 

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

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