【发布时间】:2011-04-24 17:07:54
【问题描述】:
啊,我在线程编程方面非常菜鸟,刚刚开始创建多个线程的基本步骤,所以我用谷歌搜索并得到了一些关于在 c# 中创建线程的 sn-ps,这是我找到的 sn-p:
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.");
}
}
错误是System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart)的最佳重载方法匹配有一些无效参数
为什么 Thread 构造函数要求我提供 ParameterizedThreadStart 对象,我想要传递简单的 ThreadStart 对象。
另一件事是 ThreadStart 类没有带有 1 个参数的构造函数,即它需要 0 个参数,但在 sn-p 中他们显示了 new ThreadStart(this.run) this ?
m 使用 C# 2008
这是完整的代码
使用系统; 使用 System.Threading; 类我的线程 { 公共整数计数; 公共线程 thrd; 公共MyThread(字符串名称){ 计数 = 0; thrd = new Thread(new ThreadStart(this.run)); thrd.Name = 名称; thrd.Start(); } // 线程的入口点。 无效运行(){ Console.WriteLine(thrd.Name + "starting."); 做 { 线程.睡眠(500); Console.WriteLine("In" + thrd.Name + ", 计数是 " + count); 计数++; } 而(计数【问题讨论】:
-
您能否提供一个代码示例来演示该问题?您发布的代码可以正常工作(一旦修复了明显的遗漏)。
-
@Mark:您的“完整”代码显然不完整。给我们一段绝对完整的代码,可以将它复制粘贴到一个新的文本文件中,然后进行编译。
标签: c#