안녕 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 초를 죽일해야합니다 참조하십시오. 도와주세요 ... 미리 감사드립니다.
사용하는 것이 더 합리적인 것 같다 ['System.Timers.Timer'] (http://msdn.microsoft.com/en-us/library/ system.timers.timer.aspx). –
학교 과제입니까? –
그리고 어디에 문제가 있습니까? –