[英]Creating new a Thread() is asking ParameterizedThreadStart object as a parameter in C sharp
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: 啊,我在线程编程方面非常菜鸟,只是开始了创建多个线程的基本步骤,所以我用Google搜索并获得了一些有关在c#中创建线程的摘录,这是我发现的摘录:
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 错误是System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart)的最佳重载方法匹配具有一些无效的参数
Why the Thread constructor is asking me for ParameterizedThreadStart object, i want simple ThreadStart object to be passed. 为什么Thread构造函数要求我提供ParameterizedThreadStart对象,我希望传递简单的ThreadStart对象。
Another Thing is ThreadStart class doesn't have a constructor with 1 argument ie it takes 0 arguments, but in snippet they have shown new ThreadStart(this.run)
this ? 另一件事是ThreadStart类没有带有1个参数的构造函数,即它接受0个参数,但是在代码段中它们显示了
new ThreadStart(this.run)
this? m using C# 2008 m使用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
That should be absolutely fine, because there is an overload taking ThreadStart
instead of ParameterizedThreadStart
. 这应该是绝对没问题的,因为有 过载走
ThreadStart
而不是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.");
}
}
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)
{
}
}
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. 从
run
方法组到ThreadStart
委托的转换是隐式的。
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 我怀疑您在
System.Threading.ThreadStart
和代码中其他位置定义的另一种类型之间存在名称冲突...尝试将插入符放在ThreadStart
,然后按F12转到声明
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.