0

I have created two dataGridViews and a start button as shown in the Figrue.

When I press the Start button two threads t1 and t2 are created using the delegate MyDel.

Actually, I want both dataGridView1 and dataGridView2 to be populated continuously. But only dataGridView2 is getting updated continuously. After the completion of dataGridView2, dataGridView1 starts updating.

Can anyone please give me suggestion how to fix this?

enter image description here

using System;
using System.Windows.Forms;
using System.Threading;

namespace MultiThreadedDataGridViewDemo
{
    public partial class Form1 : Form
    {
        private delegate void MyDel();

        int count=0;
        int count_1 = 0;

        public Form1()
        {    
            InitializeComponent();

            //Cells are created in dataGridView1 and dataGridView2
            for (int p = 0; p < 37; p++)
            {
                 dataGridView1.Columns.Add("", "");// dynamic cloumn adding
                 dataGridView1.Rows.Add();//dynamic row adding
                 dataGridView1.Columns[p].SortMode = DataGridViewColumnSortMode.NotSortable;//Disables the column header sorting
                 dataGridView1.Columns[p].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                 dataGridView1.Columns[p].Width = 70;

                 dataGridView2.Columns.Add("", "");// dynamic cloumn adding
                 dataGridView2.Rows.Add();//dynamic row adding
                 dataGridView2.Columns[p].SortMode = DataGridViewColumnSortMode.NotSortable;//Disables the column header sorting
                 dataGridView2.Columns[p].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                 dataGridView2.Columns[p].Width = 70;

            }
        }
        //Method to populate dataGridView1
        public void Method1()
        {
            while (count < 10000)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    for (int k = 0; k < dataGridView1.Columns.Count; k++)
                    {

                        dataGridView1.Rows[i].Cells[k].Value = count;
                    }
                }
                count++;
            }
        }
        //Method to populate dataGridView2           
        public void Method2()
        {
            while (count_1 < 10000)
            {
                for (int i = 0; i < dataGridView2.Rows.Count; i++)
                {
                    for (int k = 0; k < dataGridView2.Columns.Count; k++)
                    {
                        dataGridView2.Rows[i].Cells[k].Value = count_1;
                    }
                }
                count_1++;
            }                  
        }

        //Creates threads to populate dataGridView1 and dataGridView1
        private void btn_Start_Click(object sender, EventArgs e)
        {
                MyDel del_1 = new MyDel(Method1);
                Thread t1 = new Thread(new ThreadStart(del_1));
                t1.Start();

                MyDel del_2 = new MyDel(Method2);
                Thread t2 = new Thread(new ThreadStart(del_2));
                t2.Start();
        }
    }
}
harishli2020
  • 305
  • 6
  • 19
  • Am wondering how you didn't get "cross thread exception" Did you get it or you set `CheckForIllegalCrossThreadCalls = false` ? – Sriram Sakthivel Jun 02 '14 at 16:57
  • 1
    *How to populate two dataGridViews continously using different threads in C#?* My answer: No don't do that, UI should not be updated with worker thread. – Sriram Sakthivel Jun 02 '14 at 16:59
  • @Sriram Sakthivel No, I didn't set CheckForIllegalCrossThreadCalls = false ? I have posted my entire code. – harishli2020 Jun 02 '14 at 17:13
  • 1
    Use a [`BackgroundWorker`](http://stackoverflow.com/questions/5483565/); it is specifically designed to do what you are attempting. – Dour High Arch Jun 02 '14 at 18:00

1 Answers1

0

You should use BackgroundWorker, and update the UI in it's ProgressChanged-event, which is used to access the UI-thread. With ReportProgress-method you can send int percentProgress and object userState to the linked ProgressChanged-method. With userState-object you can, for example, pass a list full of DataGridViewRows and add them in the ProgressChanged-method to your DataGridView.

W0lfw00ds
  • 2,018
  • 14
  • 23