I have a windows form which is taking forever to load data into my datagridview.
So I thought Id use a thread but I keep getting the error:
"Cross-thread operation not valid: Control 'dataGridView1' accessed from a thread other than the thread it was created on."
Here is my code:
private void Form1_Load(object sender, EventArgs e)
{
//lblFormDisplayStatus.Text = "Retrieving Data from Database";
if (isProcessRunning)
{
MessageBox.Show("A process is already running.");
return;
}
SetIndeterminate(true);
Thread backgroundThread = new Thread(
new ThreadStart(() =>
{
isProcessRunning = true;
Thread.Sleep(5000);
//MessageBox.Show("Thread completed!");
BeginInvoke(new Action(() => Close()));
RunProgram(); // the method responsible for binding the data to datagrid
isProcessRunning = false;
}
));
backgroundThread.Start();
ShowDialog();
}
pls put me out of my misery and show me where I am going wrong
thanks