2014-02-16 5 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 : (Preffered)는 기본 형태로 해당 이벤트에 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); 
      } 
     } 
    } 

수신 거부 ion 2 : PermeabilityTest을 기본 폼의 내부 클래스로 만들면 기본 폼의 개인 멤버에 액세스 할 수 있습니다. 그런 다음 기본 폼에 대한 참조를 Permeability Testest의 생성자에 전달하고 멤버로 유지해야합니다.

옵션 3 :의 생성자에 목록 상자를 전달 PermeabilityTest

다른 스레드에서 실행되기 때문에 컨트롤에 호출을 사용하는 것을 잊지 마십시오.