2

I'm trying to create a loop which creates a thread for each program in a list, but i'm getting a "method name expected" error upon passing perimeters on the code below;

for (i = 0; i <= programs.Count; i++)
{
    checkProcess check = new checkProcess();
    // check.isRunning();

    string filename = programs[i].Filename;
    string filepath = programs[i].Filepath;

    mWorkerThread = new Thread(new ThreadStart(check.isRunning(filename, filepath)));

    mWorkerThread.Start();
}

I read a little on delegates but couldn't seem to get them to work in the context of my problem. Any help would be greatly appreciated as to what direction i should be heading.

Erik Dietrich
  • 6,080
  • 6
  • 26
  • 37
Kestami
  • 2,045
  • 3
  • 32
  • 47

2 Answers2

12

The thread target ought to be something executable and not the result of your method.

mWorkerThread = new Thread(new ThreadStart(check.isRunning(filename, filepath)));

In your case above, you try to create a new instance of ThreadStart with the return value of check.IsRunning(...). What you want is something like

mWorkerThread = new Thread( () => check.isRunning(filename, filepath) );
Matten
  • 17,365
  • 2
  • 42
  • 64
2

In your statement mWorkerThread = new Thread(new ThreadStart(check.isRunning(filename, filepath))); check.isRunning is the method name that called on the start of the thread.

Thread t = new Thread(new ThreadStart(ThreadMethod));
t.Start("My Parameter");

// method that will be called
    private void ThreadMethod(object parameter)
    {
        // parameter equals to "My Parameter"
    }

Another expect is the anonymous delegate method that make your method inline.. using lambda expression:

   Thread t = new Thread(new ThreadStart(()=>ThreadMethod(parmaValue) ));
    t.Start("My Parameter");

Ref: ThreadStart with parameters

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75