R.Vector
R.Vector

Reputation: 1719

C# : Method name expected

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.

Upvotes: 6

Views: 28455

Answers (4)

Ani
Ani

Reputation: 113402

The problem is here:

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

The argument is a method-call trying 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.

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 ParameterizedThreadStart delegate-type and the associated overloads of the Thread constructor, but it's a little awkward to work with since the argument is object, so a cast somewhere or the other will be unavoidable.

Upvotes: 6

Tudor
Tudor

Reputation: 62439

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 string parameter, you can use:

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

Upvotes: 11

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107528

The ThreadStart constructor is expected your code to look like:

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

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 190945

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

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

Upvotes: 3

Related Questions