我有计算某个文件夹中文件的方法:
private void countfiles(string path)
{
if (path != "")
{
DirectoryInfo dir = new DirectoryInfo(path);
foreach (FileInfo filesindires in dir.GetFiles())
{
if (filesindires.FullName != Application.ExecutablePath)
{
num_files++;
Thread.Sleep(1);
}
}
foreach (DirectoryInfo dirsinfolder in dir.GetDirectories())
{
countfiles(dirsinfolder.FullName);
}
}
}
当用户点击计数按钮时,我想创建一个线程,这样程序就不会挂起。
Thread count = new Thread(new ThreadStart(countfiles(@"E:/test")));
但我什至在调试之前就得到了这个错误:
Method Name Expected
我不明白;这个错误需要我做什么?
最后非常感谢您的提前帮助。
最佳答案
是
Thread count = new Thread(new ParameterizedThreadStart(countfiles));
count.Start(@"E:/test");
您不必传递参数,只需传递方法名称即可。
此外,您还需要将参数的类型更改为object
,而不是string
。或者,如果你想保留 string
参数,你可以使用:
Thread count = new Thread(
o =>
{
countFiles((string)o);
});
count.Start(@"E:/test");
关于C# : Method name expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8993371/