简体   繁体   中英

Update DataGridView cells from different thread in C# winforms

How can I update each DataGridView cell from a different thread? Basically, I have 3 threads (main thread "winform", thread 1 and thread 2), moreover, thread 1 and thread 2 are in different class (class 2) and main thread in class 1. I made this work in one class, but I couldn't do in different class. Please any help would be appreciated. Please see the code below which works in one class:

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

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

this is class2 where i would like to update the datagridview

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM