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
}
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.
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 would like to know if there is a way to dynamically change the parameters passed to a method when it running in a thread.
For example:
trdColCycl thread = new Thread (() => this.ColorsCycling (true));
trdColCycl.Start();
or
trdColCycl thread = new Thread (new ParameterizedThreadStart (this.ColorsCycling));
trdColCycl.Start(true);
and then I want to pass as a parameter to the thread running the value false ... is it possible?
(in this example I would like to dynamically change the parameter value to exit from a loop inside the thread without using global variables)
Thanks for your help.
You could create a shared variable meant for communicating between two threads
class ArgumentObject
{
public bool isOk;
}
// later
thread1Argument = new ArgumentObject() { isOk = true };
TrdColCycl thread = new Thread (() => this.ColorsCycling (thread1Argument));
trdColCycl.Start();
// much later
thread1Argument.isOk = false;
Edit:
Alternatively, you could pass the bool as reference instead:
bool isOk = true;
TrdColCycl thread = new Thread (() => this.ColorsCycling (ref isOk));
trdColCycl.Start();
// later
isOk = false;
In both cases, you'll have to modify the signature of your method:
// original
void ColorsCycling(bool isOk)
// should be
void ColorsCycling(ArgumentObject argument) // for option 1
// or
void ColorsCycling(ref bool isOk) // for option 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.
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
I've been trying to pass an object to my main thread process but it seems it will not work in the way I thought it would.
First I create the Thread:
Thread thrUDP;
Then I create the object I will use to store the data I need:
UDPData udpData;
Now I Initialize the object withthe correct data, Set up the new thread and start it with the object passed into the Start() method:
udpData = new UDPData("224.5.6.7", "5000", "0", "2");
thrUDP = new Thread(new ParameterizedThreadStart(SendStatus));
thrUDP.Start(udpData);
This is the method I wish to start:
private void SendStatus(UDPData data)
{
}
I remember using Threads a while back and I'm sure they weren't so difficult to pass data to, am I doing this the wrong way or am I just missing a piece of code?
Thanks!
The ParameterizedThreadStart delegate is declared as:
public delegate void ParameterizedThreadStart(object obj);
Clearly, this delegate isn't compatible with your method's signature, and there isn't a direct way to get a System.Threading.Thread to work with an arbitrary delegate-type.
One of your options would be to use a compatible signature for the method, and cast as appropriate:
private void SendStatus(object obj)
{
UDPData data = (UDPData)obj;
...
}
The other option would be to punt the problem to the C# compiler, creating a closure. For example:
new Thread(() => SendStatus(udpData)).Start();
Do note that this uses the ThreadStart delegate instead. Additionally, you should be careful with subsequently modifying the udpData local, since it is captured.
Alternatively, if you don't mind using the thread-pool instead of spawning your own thread, you could use asynchronous delegates. For example:
Action<UDPData> action = SendStatus;
action.BeginInvoke(udpData, action.EndInvoke, null);
private void SendStatus(object data)
{
UDPData myData = (UDPData) data;
}