2012-01-16 160 views
1

如何從不同的線程更新每個DataGridView單元格?基本上,我有3個線程(主線程「winform」,線程1和線程2),此外,線程1和線程2在不同的類(2級)和主線程在1級。我做這個工作在一個類,但我不能在不同的課堂上做。請任何幫助,將不勝感激。請參考下面這工作在一個類中的代碼:從C#winforms中的不同線程更新DataGridView單元格

// 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中,其中我想更新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 
    } 
} 
} 
+0

Downvote用於詢問什麼是StackOverflow最常見的問題的變體。 – Fantius 2012-01-16 22:54:45

回答

0

什麼你想要做的是將消息委託給擁有該控件的線程,讓它使用與之通信的數據更新表單。

相關問題