从C#winforms中的不同线程更新DataGridView单元格

本文关键字:线程 更新 DataGridView 单元格 C#winforms | 更新日期: 2023-09-27 18:20:24

如何从不同的线程更新每个DataGridView单元格?基本上,我有3个线程(主线程"winform"、线程1和线程2),此外,线程1和2在不同的类(类2)中,主线程在类1中。我在一个班上完成了这项工作,但在另一个班我做不到。请提供任何帮助,我们将不胜感激。请参阅下面的代码,它在一个类中工作:

// THIS IS THE DESIGNER FORM CLASS
namespace DelegatesAndCallback
{
partial class class1
{
    /// <summary>
    /// Variable nécessaire au concepteur.
    /// </summary>
    private System.ComponentModel.IContainer components = null;
    /// <summary>
    /// Nettoyage des ressources utilisées.
    /// </summary>
    /// <param name="disposing">true si les ressources managées doivent être supprimées ; sinon, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
    #region Code généré par le Concepteur Windows Form
    /// <summary>
    /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
    /// le contenu de cette méthode avec l'éditeur de code.
    /// </summary>
    private void InitializeComponent()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.pb = new System.Windows.Forms.ProgressBar();
        this.dgv = new System.Windows.Forms.DataGridView();
        this.Number = new System.Windows.Forms.DataGridViewTextBoxColumn();
        ((System.ComponentModel.ISupportInitialize)(this.dgv)).BeginInit();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(203, 34);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 0;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // pb
        // 
        this.pb.Location = new System.Drawing.Point(16, 415);
        this.pb.Name = "pb";
        this.pb.Size = new System.Drawing.Size(443, 23);
        this.pb.TabIndex = 2;
        // 
        // dgv
        // 
        this.dgv.AllowUserToAddRows = false;
        this.dgv.AllowUserToDeleteRows = false;
        this.dgv.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dgv.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.Number});
        this.dgv.Location = new System.Drawing.Point(167, 76);
        this.dgv.Name = "dgv";
        this.dgv.Size = new System.Drawing.Size(146, 320);
        this.dgv.TabIndex = 3;
        // 
        // Number
        // 
        this.Number.HeaderText = "Number";
        this.Number.Name = "Number";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(479, 450);
        this.Controls.Add(this.dgv);
        this.Controls.Add(this.pb);
        this.Controls.Add(this.button1);
        this.Name = "Form1";
        this.Text = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.dgv)).EndInit();
        this.ResumeLayout(false);
    }
    #endregion
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.ProgressBar pb;
    private System.Windows.Forms.DataGridView dgv;
    private System.Windows.Forms.DataGridViewTextBoxColumn Number;
}
}

这是一类主线程

// THIS IS CLASS1 MAIN THREAD
using System;
using System.Collections;
using System.Threading;
using System.Windows.Forms;
namespace DelegatesAndCallback
{
public partial class class1 : Form
{
    private Thread newThread1;
    private Thread newThread2;
    private int i = 0;
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        dgv.Paint += dgv_Paint;
        // thread #1
        newThread1 = new Thread(Process1);
        newThread1.Name = "Thread 1";
        newThread1.Start();
        newThread1.Join();
        // thread #2
        newThread2 = new Thread(Process2);
        newThread2.Name = "Thread 2";
        newThread2.Start();
        newThread2.Join();
    }
    private void dgv_Paint(object sender, PaintEventArgs e)
    {
        //Refresh();
    }
    public void Process1()
    {
        for (; i < 10; i++)
        {
            int i1 = i;
            dgv.BeginInvoke(new MethodInvoker(() => dgv.Rows.Add(i1,i1)));
            pb.BeginInvoke(new MethodInvoker(() => updateValue(i1)));
            dgv.BeginInvoke(new MethodInvoker(() => dgv.Update()));
            dgv.BeginInvoke(new MethodInvoker(() => pb.Refresh()));
            Thread.Sleep(200);
        }
    }
    public void updateValue(double i1)
    {
        pb.Value = (int)(100*(i1/19));
    }
    public void Process2()
    {
        for (; i < 20; i++)
        {
            int i1 = i;
            dgv.BeginInvoke(new MethodInvoker(() => dgv.Rows.Add(i1, i1)));
            pb.BeginInvoke(new MethodInvoker(() => updateValue(i1)));
            dgv.BeginInvoke(new MethodInvoker(() => dgv.Update()));
            dgv.BeginInvoke(new MethodInvoker(() => pb.Refresh()));
            Thread.Sleep(200);
        }
    }
}
}

这是第2类,我想在其中更新数据网格视图

using System;
using System.Collections;
using System.Threading;
using System.Windows.Forms;
namespace DelegatesAndCallback
{
class DelegateExample
{
    // Declaration
    public void Process1(){
      // HERE NEED TO HELP TO UPADTE/REFRESH AND SET DATEGRIDVIEW VALUE FROM CLASS1  
    }
    public void Process2(){
      // HERE NEED TO HELP TO UPADTE/REFRESH AND SET DATEGRIDVIEW VALUE FROM CLASS1  
    }
}
}

从C#winforms中的不同线程更新DataGridView单元格

您想要做的是将消息委托回拥有控件的线程,让它用您向其发送消息的数据更新表单。