C# – Creating new a Thread() is asking ParameterizedThreadStart object as a parameter in C sharp

c

ah i'm very noob in Thread Programming and just started a basic step to create multiple threads, so i googled and got some snippets about creating Thread in c#, here is the snippet i found:

public MyThread(string name) { 
    count = 0; 
    thrd = new Thread(new ThreadStart(this.run)); // here m getting error
    thrd.Name = name; 
    thrd.Start(); 
  } 

  // Entry point of thread. 
  void run() { 
    Console.WriteLine(thrd.Name + " starting."); 

    do { 
      Thread.Sleep(500); 
      Console.WriteLine("In " + thrd.Name + 
                        ", count is " + count); 
      count++; 
    } while(count < 10); 

    Console.WriteLine(thrd.Name + " terminating."); 
  } 
} 

The error is The best overloaded method match for System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart) has some invalid arguments

Why the Thread constructor is asking me for ParameterizedThreadStart object, i want simple ThreadStart object to be passed.

Another Thing is ThreadStart class doesn't have a constructor with 1 argument i.e. it takes 0 arguments, but in snippet they have shown new ThreadStart(this.run) this ?
m using C# 2008

Here is the complete code


using System; 
using System.Threading; 

class MyThread { 
  public int count; 
  public Thread thrd; 

  public MyThread(string name) { 
    count = 0; 
    thrd = new Thread(new ThreadStart(this.run)); 
    thrd.Name = name; 
    thrd.Start(); 
  } 

  // Entry point of thread. 
  void run() { 
    Console.WriteLine(thrd.Name + " starting."); 

    do { 
      Thread.Sleep(500); 
      Console.WriteLine("In " + thrd.Name + 
                        ", count is " + count); 
      count++; 
    } while(count 

Best Answer

Not sure why it doesn't work, but you can try that:

thrd = new Thread(run);

The conversion from the run method group to a ThreadStart delegate is implicit.

I suspect you have a name conflict between System.Threading.ThreadStart and another type defined somewhere else in your code... Try to put the caret on ThreadStart and press F12 to go to the declaration