Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 555
R.Vector Avatar asked Jan 24 '12 20:01

R.Vector


People also ask

What is C in simple words?

C Introduction C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Why is C named so?

Because a and b and c , so it's name is C. C came out of Ken Thompson's Unix project at AT&T. He originally wrote Unix in assembly language. He wrote a language in assembly called B that ran on Unix, and was a subset of an existing language called BCPL.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

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");
like image 181
Tudor Avatar answered Oct 12 '22 05:10

Tudor


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.

like image 31
Ani Avatar answered Oct 12 '22 07:10

Ani