C #: method name expected

I have this method that counts files in a specific 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 the counter button, I need to create a thread, so the program does not hang.

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

But I get this error even before debugging:

Method Name Expected

I do not understand; What is this error required of me?

Finally, thanks for your help in advance.

+5
source share
4 answers

it

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

You do not need to pass parameters, just the name of the method.

You will also need to change the type of the parameter to object, rather than string. Alternatively, if you want to save the parameter string, you can use:

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

The problem is here:

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

- -, . , - , .

:

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

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

, :

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

:

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

ParameterizedThreadStart Thread, , object, - .

+6

ParameterizedThreadStart. .

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

ThreadStart , :

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

, , . , :

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

All Articles