2012-11-12 5 views
0

안녕 sleeptime 및 스레드 개체를 메서드에 전달하고 for 루프에서 메서드를 호출하고 싶습니다. Pls는이for 루프에서 스레드를 호출하는 방법

public delegate void PasParamsToThrdFunc(int integer, object obj2); 
class Program 
{ 
    Thread[] newThread=new Thread[10]; 

    static void Main(string[] args) 
    { 
     Program pr = new Program(); 
     pr.ThreadDeclaration(); 
     Console.Read(); 
    } 

    public void ThreadDeclaration() 
    { 
     int time = 5000; 
     for(int i=1;i<3;i++) 
     { 
      time = time * i; 
      string s = i.ToString(); 
      ThreadStart starter =() => PasParamsToThrdFunc(time, newThread[i]); 
      newThread[i] = new Thread(starter); 
      newThread[i].Name = i.ToString(); 
      newThread[i].Start(); 
     } 

    } 

    public void PasParamsToThrdFunc(int waitTime, Thread obj) 
    { 
     Thread.Sleep(waitTime); 
     Console.WriteLine("" + waitTime + " seconds completed and method is called for thread"+obj.Name+""); 
     obj.Abort(); 
    } 
    } 

아래의 코드는 내가 1 실 5 초 후에 호출하고 객체를 죽일하며 2 스레드에 대한 동일한 기능을 수행 할 10 초를 죽일해야합니다 참조하십시오. 도와주세요 ... 미리 감사드립니다.

+0

사용하는 것이 더 합리적인 것 같다 ['System.Timers.Timer'] (http://msdn.microsoft.com/en-us/library/ system.timers.timer.aspx). –

+0

학교 과제입니까? –

+0

그리고 어디에 문제가 있습니까? –

답변

0

1- newThread[i]에서 PasParamsToThrdFunc으로 전달할 때 null입니다. 당신은 closing over 변수 i과 몇 가지 문제가 있습니다 time

public void ThreadDeclaration() 
{ 
    int time = 5000; 
    for (int i = 1; i < 3; i++) 
    { 
     int J = i; // <---- 
     int timez = time * i; // <---- 
     string s = i.ToString(); 

     ThreadStart starter =() => PasParamsToThrdFunc(timez, J); 
     newThread[i] = new Thread(starter); 
     newThread[i].Name = i.ToString(); 
     newThread[i].Start(); 
    } 

} 

public void PasParamsToThrdFunc(int waitTime, int i) 
{ 
    Thread.Sleep(waitTime); 
    Console.WriteLine("" + waitTime + " seconds completed and method is called for thread" + newThread[i].Name + ""); 
    newThread[i].Abort(); // <-- No need 
} 
0

을 피하기 위해해야 ​​할 수도 있습니다 i

2로 변경 내가 볼 수 있습니다

  • 을 당신은 매개 변수를 수정 폐쇄에 액세스하는 스레드 함수에 전달 중입니다 (매개 변수를 전달한 후에 매개 변수를 수정하면 안됩니다).
  • 스레드 핸들을 자체에 전달한 다음 스레드 핸들을 사용하여 자체를 중단합니다. 그럴 필요가 없습니다. 잠이 든 후에 방금 끝날 것이기 때문입니다.
  • 시간 간격 * i를 사용하는 대신 각 반복에서 i를 곱하여 시간을 잘못 계산합니다. 대신이 같은

시도 뭔가 :

using System; 
using System.Threading; 

namespace Demo 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Program pr = new Program(); 
      pr.ThreadDeclaration(); 
      Console.Read(); 
     } 

     public void ThreadDeclaration() 
     { 
      int timeInterval = 5000; 

      for (int i=1; i<3; i++) 
      { 
       int time = timeInterval * i; 
       ThreadStart starter =() => PasParamsToThrdFunc(time); 
       var thread = new Thread(starter) {Name = i.ToString()}; 
       thread.Start(); 
      } 
     } 

     public void PasParamsToThrdFunc(int waitTime) 
     { 
      Thread.Sleep(waitTime); 
      Console.WriteLine("" + waitTime + " seconds completed and method is called for thread" + Thread.CurrentThread.Name); 

     } 
    } 
}