2014-12-07 31 views
-1

這是我的代碼,但label1在label2之前先運行?我想要的是在各自的線程上同時運行label1和label2。我使用線程的方式,但它不能訪問控制,除了在它自己創建的線程中。所以在這段代碼中,當我創建如下的實例時:Slave s1 = new Slave(label1); Slave s2 = new Slave(label2);它會自動開始移動這兩個標籤,但不是。使用backgroundworker同時向下移動2個標籤

public class Slave 
{ 
    private Label l; 
    private BackgroundWorker bw; 
    public Slave(Label l) 
    { 
     this.l = l; 
     bw = new BackgroundWorker(); 
     bw.WorkerReportsProgress = true; 
     bw.DoWork+=new DoWorkEventHandler(worknow); 
     bw.ProgressChanged += new ProgressChangedEventHandler(update); 
     bw.RunWorkerAsync(); 
    } 
    private void worknow(object sender, DoWorkEventArgs e) 
    { 
     BackgroundWorker b = sender as BackgroundWorker; 
     b.ReportProgress(1); 
    } 
    private void update(object sender, ProgressChangedEventArgs e) 
    { 
     for(int x=0; x<20;x++) 
     { 
      l.Top += 10; 
      System.Threading.Thread.Sleep(100); 
     } 
    } 

} 
+0

當您搜索收到的例外消息的確切文本時,您發現了什麼?您是否嘗試納入您找到的任何建議?你做了什麼事? – 2014-12-07 01:59:08

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 – 2014-12-07 02:01:38

+0

另一個線程可以修改GUI元素:http://stackoverflow.com/questions/527947/invoke-required – 2014-12-07 02:02:31

回答

0

好吧,我剛剛找到了解決我的問題,所以我會回答我的問題,所以我有1個面板和按鈕,這樣簡單的一個winform,所以我有一個類動畫,使得動畫一= new Animate(panel1,startingX,startingY,newX,newY);實例將在起始位置中一個按鈕的點擊創建節點,並將其移動到所需的新的位置每隔100毫秒類節點的定義如下所示:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    class Node:Label 
    { 
     public Node(int x, int y) 
     { 
      this.Top = y; 
      this.Left = x; 
     } 
     public void updatePos(int x, int y) 
     { 
      this.Top += y; 
      this.Left += x; 
     } 
    } 
} 

那麼這是動畫的定義類:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Threading; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     Animate a = new Animate(panel1,0,0,1,1); //this is the actual instatiation test 
     Animate b = new Animate(panel1, 100, 0,2,2); //for node b 

    } 

    public class Animate 
    { 
     private Node l; 
     Thread th; 
     private delegate void cl(int x, int y); 
     Delegate del; 
     private int a, b; 

     public Animate(Panel p, int x, int y, int a, int b) 
     { 
      l = new Node(0,0); 
      del = new cl(l.updatePos); 
      this.a = a; 
      this.b = b; 

      this.l.AutoSize = true; 
      this.l.Location = new System.Drawing.Point(x, y); 
      this.l.Name = "label1"; 
      this.l.Size = new System.Drawing.Size(35, 13); 
      this.l.TabIndex = 0; 
      this.l.Text = "label1"; 
      p.Controls.Add(l); 

      th = new Thread(thread); 
      th.Start(); 
     } 
     private void thread() 
     { 
      while (true) 
      { 
       try 
       { 
        l.Invoke(del, a, b); 
        Thread.Sleep(100); 
       } 
       catch (Exception e) 
       { 
        return; 
       } 
      } 
     } 
    } 
} 

}

有些人可能會想知道什麼我需要這個了,好,我想實現只用純粹的C#編碼的winform應用二叉搜索樹和AVL模擬器,這就是爲什麼林調用標籤爲「節點」