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.

2 Answers2

0

1- when you pass newThread[i] to PasParamsToThrdFunc it is null. Change it to i

2- You may need to avoid closing over variables i and 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
}
Community
  • 1
  • 1
L.B
  • 114,136
  • 19
  • 178
  • 224
0

There are a few problems I can see:

  • You are accessing modified closures with the parameters you are passing to the thread function (you shouldn't modify those params after passing them).
  • You passing a thread's handle to itself and then using it to abort itself. No need to do that, since it will just exit when after the sleep.
  • You are calculating time incorrectly, by multiplying itself by i on each iteration, rather than using the time interval * i.

Try something like this instead:

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

        }
    }
}                                                                                            
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276