如何在C#中使用不同的线程连续填充两个dataGridView? [英] How to populate two dataGridViews continously using different threads in C#?

查看:130
本文介绍了如何在C#中使用不同的线程连续填充两个dataGridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个dataGridViews和一个开始按钮。



当我按下开始按钮时,使用代理MyDel创建了两个线程t1和t2。



实际上,我希望连续填充dataGridView1和dataGridView2。但只有dataGridView2会不断更新。 dataGridView2完成后,dataGridView1开始更新。



任何人都可以给我建议如何解决这个问题?



I have created two dataGridViews and a start button.

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?

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();
            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;

            }
        }
        //Populates 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++;
            }
        }
        //Populates 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++;
            }                  
        }

        //Starts the 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();
        }
    }
}

推荐答案

您无法从非UI调用与UI相关的任何内容线。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA
You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于如何在C#中使用不同的线程连续填充两个dataGridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆