Reputation: 2073
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.
Upvotes: 2
Views: 1060
Reputation: 18290
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
Upvotes: 2
Reputation: 17631
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) );
Upvotes: 12