我正在尝试创建一个循环,为列表中的每个程序创建一个线程,但在下面的代码中传递边界时出现“预期方法名称”错误;
for (i = 0; i <= programs.Count; i++)
{
checkProcess check = new checkProcess();
// check.isRunning();
string filename = programs[i].Filename;
string filepath = programs[i].Filepath;
mWorkerThread = new Thread(new ThreadStart(check.isRunning(filename, filepath)));
mWorkerThread.Start();
}
我阅读了一些关于委托(delegate)的内容,但似乎无法让他们在我的问题上下文中工作。对于我应该朝哪个方向提供任何帮助,我们将不胜感激。
最佳答案
线程目标应该是可执行的而不是你的方法的结果。
mWorkerThread = new Thread(new ThreadStart(check.isRunning(filename, filepath)));
在您上面的例子中,您尝试使用 check.IsRunning(...)
的返回值 创建 ThreadStart
的新实例>。你想要的是像
mWorkerThread = new Thread( () => check.isRunning(filename, filepath) );
关于c# multithreading 方法名称预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10176531/