C#:应为方法名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8993371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:29:41  来源:igfitidea点击:

C# : Method name expected

c#multithreadingmethods

提问by R.Vector

I have that method that counts files in a certain folder:

我有一种计算某个文件夹中文件的方法:

    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);
            }
        }           
    }

and when the user clicks on the count button I wanted to make a thread, so the program doesn't hang.

当用户单击计数按钮时,我想创建一个线程,因此程序不会挂起。

Thread count = new Thread(new ThreadStart(countfiles(@"E:/test")));

But I get this error even before debugging:

但我什至在调试之前就收到了这个错误:

Method Name Expected

I dont understand; what does that error need from me?

我不明白;这个错误需要我做什么?

Finally thanks a lot for your help in advance.

最后非常感谢您的帮助。

采纳答案by Tudor

It's

它是

Thread count = new Thread(new ParameterizedThreadStart(countfiles));    
count.Start(@"E:/test");

You don't have to pass the parameters, just the method name.

您不必传递参数,只需传递方法名称。

Also you will need to change the type of the parameter to object, not string. Alternatively, if you want to keep the stringparameter, you can use:

您还需要将参数的类型更改为object,而不是string。或者,如果要保留string参数,可以使用:

Thread count = new Thread(
   o => 
   {
       countFiles((string)o);    
   });
count.Start(@"E:/test");

回答by Daniel A. White

Have a look at the ParameterizedThreadStartdelegate. This will pass the values for you.

看看ParameterizedThreadStart代表。这将为您传递值。

Thread count = new Thread(countfiles); 
count.Start(@"E:/test");

回答by C???

The ThreadStart constructor is expected your code to look like:

ThreadStart 构造函数预计您的代码如下所示:

Thread count = new Thread(new ThreadStart(countfiles));
count.Start();

It needs to know which method to execute, not the result of the method. But since you have a parameter, you need to do it like this:

它需要知道执行哪个方法,而不是方法的结果。但既然你有一个参数,你需要这样做:

Thread count = new Thread(new ParameterizedThreadStart(countFiles));
count.Start(@"E:/test");

回答by Ani

The problem is here:

问题在这里:

new ThreadStart(countfiles(@"E:/test"))

The argument is a method-calltrying to masquerade as a method-group. The compiler can turn a compatible method-group, lambda expression or anonymous method to a delegate-type but not a method call.

参数是一个方法调用,试图伪装成方法组。编译器可以将兼容的方法组、lambda 表达式或匿名方法转换为委托类型,但不能转换为方法调用。

Try one of these :

尝试以下方法之一:

// Lambda
var thread = new Thread(() => countfiles(@"E:/test")); 

// Anonymous method
var thread = new Thread( delegate() { countfiles(@"E:/test"); }); 

If you want to use a method-group, you'll need a separate method:

如果要使用方法组,则需要一个单独的方法:

private void CountTestFiles()
{
     countFiles(@"E:/test");
}

and then you can do:

然后你可以这样做:

// Method-group
var thread = new Thread(CountTestFiles) 

You could also work with the ParameterizedThreadStartdelegate-type and the associated overloads of the Threadconstructor, but it's a little awkward to work with since the argument is object, so a cast somewhere or the other will be unavoidable.

您还可以使用ParameterizedThreadStart委托类型和Thread构造函数的关联重载,但由于参数是object,因此使用起来有点尴尬,因此在某处或其他地方进行强制转换将是不可避免的。