0

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

  • `pls put me out of my misery and show me where I am going wrong` - You might be interested in watching [this short clip](http://www.youtube.com/watch?v=D3Y6DnFpHCA). – Federico Berasategui Jul 28 '14 at 13:07

1 Answers1

0

UI Controls are thread agnostic (need to be updated on a thread which created the control). In your case, you are running a new thread which is calling a method which seems to be updating a UI control (dataGridView1).

As a quick reference: read about the Control.BeginInvoke (MSDN).

Plus, this question is similar to yours: Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on

Edit: The method/statement which is update DataGridView needs to be run on UI thread. Something like this:

private void  RunProgram()
{
  //Do time consuming work here (non-UI thread)
   dataGridView1.BeginInvoke(new InvokeDelegate(AttachData));
}
private void AttachData()
{
   //dataGridView1.Table.... Bind data here (this method would be executed on UI thread)
}
Community
  • 1
  • 1
Manish Basantani
  • 16,931
  • 22
  • 71
  • 103
  • On formLoad, I display the form with a progress bar so user knows something is happening and to wait. I just need to know where to place my method??? – CaughtInALoop Jul 28 '14 at 13:18
  • got it!!! all I did was remove Close())) from the above method and replace it with RunProgram() and it works ..... – CaughtInALoop Jul 29 '14 at 12:59
  • If my answer helped you in getting there, you can accept it and that would mark your question as answered. That would help others with similar problem. – Manish Basantani Aug 01 '14 at 09:01
  • thank you....to be honest i did not use your suggestion at all, I simply changed one part of my code as mentioned above, so your suggestion could possibly confuse someone...sorry – CaughtInALoop Aug 01 '14 at 21:41