I get this error when I try to run my code
"Cross-thread operation not valid:Control 'dataGridView1'accesses from a thread other than the thread it was created on"

File Name Form.cs

I have method call FillDataGrid()

FillDataGrid(string msg)
string[] data = msg.split(';');
string Name = data[0];
string LastName = data[1];

I call this method in a another file call Project.cs

Public class Project
Form1 al = new Form1();
ThreadStart th;
Thread thread;

private void Project_Startup(object sender, EventArgs e)
private void StartClentConnect()
            //connect to client
            th = new ThreadStart(Recieve);
            thread = new Thread(th);


 public void Recieve()
//connection related code
                        byte[] buffer = new byte[1024];
                        string msg = Encoding.UTF8.GetString(buffer);
                        string[] data = msg.Split(';');
                        if (data.Length > 0)


I get error wen the debugger reaches
dataGridView1.Rows.Add(Name,LastName); form file form1.cs

Help please havent worked on threads before..

UI objects can only be accessed on the thread that creates them (usually the startup thread). You can check if you are on the UI thread or not by calling InvokeRequired on the control you wish to update. If it is, then you'll need to Invoke a delegate on the UI thread and have it perform the change. Searching for Invoke using your favorite search engine will turn up a lot of examples of how this is done (as will reading the MSDN documentation on Control.Invoke)

This article has been dead for over six months. Start a new discussion instead.