2014-02-16 36 views
-1

我搜索了並找不到解決方案,它可以幫助我將文本從運行在單獨的類中的線程返回到創建線程的窗體上的列表框。從單獨的線程/類設置控制屬性

基本上我有一個持有「測試」的類,它從測試窗口自己的線程中調用。我想要做的就是將文本添加到主窗體上的列表框中,讓用戶知道測試正在進行的是什麼。我可以在Invoke上找到的所有示例都顯示瞭如何在同一個類中執行此操作。

當我啓動線程:

PermeabilityTest Run_Test = new PermeabilityTest(); 
    public Thread WorkerThread; 

    private void button2_Click(object sender, EventArgs e) 
    { 
     //enable timer for test duration display 
     timer1.Enabled = true; 

     //create and start new thread. 
     WorkerThread = new Thread(Run_Test.RunTest); 
     WorkerThread.Start(); 
    } 

這裏是我的類,它實際做的工作,我需要讓文本回一個列表框從單獨的形式。

public class PermeabilityTest 
{ 
    //volatile alerts the compiler that it will be used across threads. 
    private volatile bool aborted; 

    public void RequestStop() 
    { 
     //handle saving data file here as well. 
     aborted = true; 
    } 

    public void RunTest() 
    { 
     //reference the comms class so we can communicate with the machine 
     PMI_Software.COMMS COM = new COMMS(); 

     //some test stuffs here 
     int x = 0; 
     while(x < 100 && !aborted) 
     { 
      System.Diagnostics.Debug.Write("Well here it is, running it's own thread." + Environment.NewLine); 
      COM.Pause(1); 
     } 
    }   
} 

我希望任何一個誰可以幫助我瞭解如何獲得一些文本回到那個具有啓動線程的按鈕相同的形式在列表框。

回答

0

選項1:(較受歡迎)PermeabilityTest該事件添加事件,並註冊在你的主要形式。 然後從主窗體中修改列表框的內容。

實施例:

你的主要形式:

PermeabilityTest Run_Test = new PermeabilityTest(); 
    public Thread WorkerThread; 

    public form1() 
    { 
     // Register on the Progress event 
     Run_Test.Progress += Run_Test_Progress; 
    } 

    void Run_Test_Progress(string message) 
    { 
    if(listBox.InvokeRequired) 
     { 
      // Running on a different thread than the one created the control 
      Delegate d = new ProgressEventHandler(Run_Test_Progress); 
      listBox.Invoke(d, message); 
     } 
     else 
     { 
      // Running on the same thread which created the control 
      listBox.Items.Add(message); 
     } 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     //enable timer for test duration display 
     timer1.Enabled = true; 

     //create and start new thread. 
     WorkerThread = new Thread(Run_Test.RunTest); 
     WorkerThread.Start(); 
    } 

新代表:

public delegate void ProgressEventHandler(string message); 

改性PermeabilityTest類:

public class PermeabilityTest 
    { 
     //volatile alerts the compiler that it will be used across threads. 
     private volatile bool aborted; 
     public event ProgressEventHandler Progress; 

     public void RequestStop() 
     { 
      //handle saving data file here as well. 
      aborted = true; 
     } 

     public void RunTest() 
     { 
      //reference the comms class so we can communicate with the machine 
      PMI_Software.COMMS COM = new COMMS(); 

      //some test stuffs here 
      int x = 0; 
      while (x < 100 && !aborted) 
      { 
       // Report on progress 
       if(Progress != null) 
       { 
        Progress("This message will appear in ListBox"); 
       } 
       System.Diagnostics.Debug.Write("Well here it is, running it's own thread." + Environment.NewLine); 
       COM.Pause(1); 
      } 
     } 
    } 

選離子2: 您可以使PermeabilityTest成爲您的主窗體的內部類,並通過這樣做,允許它訪問主窗體的私有成員。 然後,您需要將主表單的引用傳遞給PermeabilityTest的構造函數,並將其保留爲成員。

方案3: 通過列表框中的構造PermeabilityTest

不要忘了,因爲你是從不同的線程中運行使用調用你的控制。