Reputation: 3911
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
Upvotes: 1
Views: 7791
Reputation: 1500505
That should be absolutely fine, because there is an overload taking ThreadStart
instead of ParameterizedThreadStart
.
I suspect there's something else at play here... could you provide a short but complete example which demonstrates the problem?
Aside from missing the class declaration itself and variable declarations, your code compiles for me with no problem:
using System;
using System.Threading;
class MyThread {
int count;
Thread thrd;
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.");
}
}
Upvotes: 2
Reputation: 292425
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
Upvotes: 2
Reputation: 2142
I have completed the incomplete example you gave us and I do not encounter the same compiler error.
class Program
{
static int count;
static Thread thrd;
public static void MyThread(string name) {
count = 0;
thrd = new Thread(new ThreadStart(run)); // here m getting error
thrd.Name = name;
thrd.Start();
}
// Entry point of thread.
static void run() {
Console.WriteLine(thrd.Name + " starting.");
do
{
Thread.Sleep(500);
Console.WriteLine("In " + thrd.Name +
", count is " + count);
count++;
} while (count == 5);
}
static void Main(string[] args)
{
}
}
Upvotes: 2