2013-03-16 40 views
0

我在c#上的應用程序工作,我有我的線程和我的用戶界面的問題...已解決:線程和調用,如何修改標籤?

我想在線程運行時在我的標籤上添加+1。問題是我不知道我怎麼能解決這個問題。我也讀了不少的「如何做」,但我的應用解決方案donc工作..

我的線程類:

class clsWorker 
    { 
     //Thread myThread = new Thread(new ThreadStart(ThreadLoop)); 

     public SerialPort port; 
     public String url; 
     Thread t; 
     clsSMS clsobjSMS = new clsSMS(); 
     SMSapplication clsobjAPP = new SMSapplication(); 

     public clsWorker(SerialPort serialPort, String urlChamp) 
     { 
      this.port = serialPort; 
      this.url = urlChamp; 
     } 


     public void StartThread() 
     { 
      t = new Thread(new ThreadStart(ThreadLoop)); 
      t.Start(); 
     } 


     public void ThreadLoop() 
     { 
      // How I can add +1 on the countSMSok label ?? 
      clsobjAPP.updateCountSMS("countSMSok"); 

     } 
    } 

我的應用等級:

public partial class SMSapplication : Form 
    { 
public void updateCountSMS(String label) 
    { 
     int num; 

      this.countSMSnok = new System.Windows.Forms.Label(); 
      this.countSMSok = new System.Windows.Forms.Label(); 

      this.Controls.Add(this.countSMSnok); 
      this.Controls.Add(this.countSMSok); 


     if (label == this.countSMSok.Name.ToString()) 
     { 
      if (int.TryParse(this.countSMSok.Text.ToString(), out num)) 
       this.countSMSok.Invoke((MethodInvoker)(() => this.countSMSok.Text = num++.ToString())); 

     } 
     else if (label == this.countSMSnok.Name.ToString()) 
     { 
      if (int.TryParse(this.countSMSnok.Text.ToString(), out num)) 
       this.countSMSnok.Invoke((MethodInvoker)(() => this.countSMSnok.Text = num++.ToString())); 
     } 
    } 

     private void btnRequestStart_Click(object sender, EventArgs e) 
    { 
     this.btnRequestStart.Enabled = false; 
     this.btnRequestStop.Enabled = true; 
     objclsWorker = new clsWorker(this.port, this.urlChecker.Text); 
     objclsWorker.StartThread(); 
    } 

} 

非常感謝您的幫助!

+1

什麼是 「不工作」 是什麼意思?請發佈任何錯誤消息/例外。 – Lennart 2013-03-16 10:50:26

+0

我已添加例外:)感謝您的幫助! – Geoffrey 2013-03-16 10:53:29

+0

嘗試在Invoke調用之前添加'IntPtr tempHandle = this.Handle;'。這訪問窗口句柄,異常抱怨。 – Lennart 2013-03-16 10:57:36

回答

0

確保您創建標籤的實例,並在標籤添加到控件

this.countSMSnok = new System.Windows.Forms.Label(); 
    this.countSMSok = new System.Windows.Forms.Label(); 

    this.Controls.Add(this.countSMSnok); 
    this.Controls.Add(this.countSMSok); 
+0

好jordanhill123!現在的應用程序沒有bug,但我的標籤沒有實現... Si編輯請:) – Geoffrey 2013-03-16 11:28:05

0

不能初始化SMSapplication類的新對象,更新它,期望它來更新您的第一種形式。那些是不同的'事物'。 此外,您應該使用SynchronizationContext而不是invoke

這裏的工作代碼:

形式:

public partial class SMSapplication : Form 
{ 
    private SynchronizationContext context = null; 
    private SerialPort port; 

    public SMSapplication() 
    { 
     InitializeComponent(); 
     this.countSMSok.Text = "0"; 
     this.context = WindowsFormsSynchronizationContext.Current; 
    } 

    public void updateCountSMS(String label) 
    { 
     this.context.Post(new SendOrPostCallback(updateCountSMSSync), label); 
    } 

    private void updateCountSMSSync(object o) 
    { 
     string label = o as string; 
     int num; 

     if (label == this.countSMSok.Name.ToString()) 
     { 
      if (int.TryParse(this.countSMSok.Text.ToString(), out num)) 
      { 
       this.countSMSok.Text = (++num).ToString(); 
      } 
     } 
    } 

    private void btnRequestStart_Click(object sender, EventArgs e) 
    { 
     clsWorker objclsWorker = new clsWorker(this, this.port, this.urlChecker.Text); 
     objclsWorker.StartThread(); 
    } 
} 

和工人:

class clsWorker 
{ 
    public SerialPort port; 
    public String url; 
    SMSapplication clsobjAPP = null; 

    public clsWorker(SMSapplication app, SerialPort serialPort, String urlChamp) 
    { 
     this.clsobjAPP = app; 
     this.port = serialPort; 
     this.url = urlChamp; 
    } 

    public void StartThread() 
    { 
     new Thread(new ThreadStart(ThreadLoop)).Start(); 
    } 


    public void ThreadLoop() 
    { 
     clsobjAPP.updateCountSMS("countSMSok"); 
    } 
} 
+0

Hooooo我的好,你是一個很好的!這是完美的工作!我明白我的愚蠢。我創建了一個新實例...感謝你,我會睡得更少愚蠢:)!謝謝你+++ – Geoffrey 2013-03-16 12:11:59