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.
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) );
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
Related
I want to run function with void as return type in new thread, but it always shows this error:
No overload for 'myVoid' matches delegate 'ThreadStart'
and my code :
Thread t = new Thread(new ThreadStart(myVoid)); // <-- Error Shows Here
t.Start("Test","Test2");
// And The Void :
void myVoid(string text, string text2)
{
Console.WriteLine(text + text2);
}
How I fix it? What is wrong?
ThreadStart delegate expects a delegate that takes no parameters. If you would like to use myVoid in a thread, you need to provide a way to make a match between myVoid and a no-argument delegate.
One way of doing it is by providing a lambda, like this:
Thread t = new Thread(new ThreadStart(() => myVoid("Test", "Test2")));
t.Start();
The ThreadStart delegate you are using does not define any arguments.
This means your method myVoid which has 2 string arguments does not match the delegate.
enter image description herehello I want to ask is there a way to create a new thread to start the function and send information to it.
something like the info i need to send to join the thread or something like that.
This is What i mean:
private Thread T1;
private Thread T2;
public void Start()
{
string NaMES = "DEMO";
int AGE = Convert.ToInt32("44");
T1 = new Thread(Here(NaMES, AGE));
T1.Start();
}
public object Here(string NAME, int AGE)
{
MessageBox.Show(NAME + AGE);
return null;
}
Thread has an overloaded constructor that allows you to pass a single parameter, so you could create an object that holds all the data required by your thread delegate.
But probably simpler is to just use a lambda to create a closure around your variables automatically:
T1 = new Thread(() => Here(NaMES, AGE));
I need to know how to send data over my threads, I have this code.
new Thread(BattleArena.ArenaGame(12)).Start();
And over BattleArena class I have
public static void ArenaGame(int test)
{
while (true)
{
Console.WriteLine(test);
Thread.Sleep(400);
}
}
But that is not a valid way...
Right now you are "sending" the result of a method call. (Not even compilable). You want to send/execute a function:
new Thread(() => BattleArena.ArenaGame(12)).Start();
Don't use parameterized threads, they are obsolete thanks to lambdas.
To clarify: a thread is not a way to send data. It is a way to execute a function. The the function has to contain the data.
You need to use parameterised threads. Like
ThreadStart start = () => { BattleArena.ArenaGame(12); };
Thread t = new Thread(start);
t.Start();
Or
Thread newThread = new Thread(BattleArena.ArenaGame);
newThread.Start(12);
then change this method as it only takes object as parameter as ThreadStart is not a generic delegate
public static void ArenaGame(object value)
{
int test = (int)value;
while (true)
{
Console.WriteLine(test);
Thread.Sleep(400);
}
}
you should use Parameterized ThreadStart
I want to start a new thread for one simple method but that method has variables I need to pass it.
Thread tempmovethread = new Thread(new ThreadStart(widget.moveXYZINCHES(xval,yval,zval));
I am getting the error: "Method name expected".
That is the right method name and I did something very similar to this in an earlier bit of code and it worked, the only difference is the method I called before didnt need any variables to be passed:
executethread = new Thread(new ThreadStart(execute.RunRecipe));
Is it possible to start a new thread and pass the variables like this, or do I have to do it another way?
Use an Action to create the correct delegate type.
Thread tempmovethreading = new Thread(new ThreadStart(new Action(() => widget.moveXYZINCHES(xval,yval,zval)));
tempmovethread = new Thread(new ParametrizedThreadStart(widget.moveXYZINCHES);
tempmovethread.Start(new []{xval,yval,zval});
BUT
you should appropriately change the method's signature like this (assuming the used parameters are of type int:
public void moveXYZINCHES(object state)
{
int xval = (state as int[])[0],yval = (state as int[])[1],zval = (state as int[])[2];
...your code
}
I am currently working on a c# project and I need to have a method which has 1 paramater to run as a thread.
E.g.
public void myMethod(string path)
{
int i = 0;
while (i != 0)
{
Console.WriteLine("Number: " + i);
i++;
}
}
How can I call the above method from another method, but running inside a thread.
Thanks for any help you can provide.
The simplest way is generally to use an anonymous method or lambda expression:
string path = ...;
Thread thread = new Thread(() => MyMethod(path));
thread.Start();
You can use a ParameterizedThreadStart, but I generally wouldn't.
Note that if you do it in a loop, you need to be aware of the normal "closing over the loop variable" hazard:
// Bad
foreach (string path in list)
{
Thread thread = new Thread(() => MyMethod(path));
thread.Start();
}
// Good
foreach (string path in list)
{
string copy = path;
Thread thread = new Thread(() => MyMethod(copy));
thread.Start();
}
new Thread(o => myMethod((string)o)).Start(param);
Simply wrap that method call in a method that takes no parameters, but which calls your method with the right parameter.
public void myWrappingMethod()
{
myMethod(this.Path);
}
public void myMethod(string path)
{
// ...
}
Or if you have lambdas available, simply use one of those (per Jon Skeet's answer).