【问题标题】:Update DataGridView cells from different thread in C# winforms从 C# winforms 中的不同线程更新 DataGridView 单元格
【发布时间】:2012-01-16 22:14:03
【问题描述】:

如何从不同的线程更新每个 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;
}
}

这是class1主线程

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

这是我想更新数据网格视图的 class2

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  
    }
}
}

【问题讨论】:

  • 投反对票,因为它提出了 StackOverflow 上最常见问题的变体。

标签: c# winforms multithreading invoke


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 2022-01-02
    • 1970-01-01
    相关资源
    最近更新 更多