0

Hi I want to pass sleeptime and the Thread Object to a method and call the method in a for loop. Pls see the code below

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();
    }
  }

I want the 1st thread to be invoked after 5 seconds and shall kill the object and shall do the same for the 2nd thread and kill it at 10 seconds. Please help... Thanks in advance.

4

2 回答 2

0

我可以看到一些问题:

  • 您正在使用传递给线程函数的参数访问修改后的闭包(传递它们后不应修改这些参数)。
  • 您将线程的句柄传递给自身,然后使用它来中止自身。不需要这样做,因为它会在睡眠后退出。
  • 您计算的时间不正确,每次迭代时将自身乘以 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);

        }
    }
}                                                                                            
于 2012-11-12T13:51:24.230 回答
0

1-当你传递newThread[i]PasParamsToThrdFunc它时为空。将其更改为i

2-您可能需要避免关闭变量itime

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
}
于 2012-11-12T13:47:55.973 回答