Start a new Function with threading - c#

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));

Related

Send data using threads

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

c# multithreading method name expected

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

C# Threadstart Error

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
}

C# Anonymous Thread with Lambda Syntax

In general I get C#'s lambda syntax. However the anonymous thread syntax isn't completely clear to me. Can someone explain what a thread creation like this is actually doing? Please be as detailed as possible, I'd love to have a sort of step-by-step on the magic that makes this work.
(new Thread(() => {
DoLongRunningWork();
MessageBox.Show("Long Running Work Finished!");
})).Start();
The part that I really don't understand is the Thread(() => ...
When I use this syntax it seems like I remove a lot of the limits of a traditional ThreadStart such as having to invoke on a method that has no parameters.
Thanks for your help!
() => ... just means that the lambda expression takes no parameters. Your example is equivalent to the following:
void worker()
{
DoLongRunningWork();
MessageBox.Show("Long Running Work Finished!");
}
// ...
new Thread(worker).Start();
The { ... } in the lambda let you use multiple statements in the lambda body, where ordinarily you'd only be allowed an expression.
This:
() => 1 + 2
Is equivalent to:
() => { return (1 + 2); }
This is anonymous way to create a thread in C# which just start the thread (because you are using Start();)
Following 2 ways are equivalent. If you need Thread variable to do something (for example block the calling thread by calling thread0.join()), then you use the 2nd one.
new Thread(() =>
{
Console.WriteLine("Anonymous Thread job goes here...");
}).Start();
var thread0= new Thread(() =>
{
Console.WriteLine("Named Thread job goes here...");
});
thread0.Start();
Now the Thread method part. If you see the Thread declaration we have the following (I omitted 3 others).
public Thread(ThreadStart start);
Thread takes a delegate as a parameter. Delegate is reference to a method. So Thread takes a parameter which is a delegate. ThreadStart is declared like this.
public delegate void ThreadStart();
It means you can pass any method to Thread which return void and doesn't take any parameters. So following examples are equivalent.
ThreadStart del = new ThreadStart(ThreadMethod);
var thread3 = new Thread(del);
thread3.Start();
ThreadStart del2 = ThreadMethod;
var thread4 = new Thread(del2);
thread4.Start();
var thread5 = new Thread(ThreadMethod);
thread5.Start();
//This must be separate method
public static void ThreadMethod()
{
Console.WriteLine("ThreadMethod doing important job...");
}
Now we think that ThreadMethod method is doing little work we can make it to local and anonymous. So we don't need the ThreadMethod method at all.
new Thread( delegate ()
{
Console.WriteLine("Anonymous method Thread job goes here...");
}).Start();
You see after delegate to last curly braces is equivalent to our ThreadMethod(). You can further shorten the previous code by introducing Lambda statement (See MSDN). This is just you are using and see how it has been ended up like the following.
new Thread( () =>
{
Console.WriteLine("Lambda statements for thread goes here...");
}).Start();
As there was some answers before I started, I will just write about how additional parameters make their way into lambda.
In short this thing called closure. Lets dissect your example with new Thread(() => _Transaction_Finalize_Worker(transId, machine, info, newConfigPath)).Start(); into pieces.
For closure there's a difference between class' fields and local variables. Thus let's assume that transId is class field (thus accessible through this.transId) and others are just local variables.
Behind the scenes if lambda used in a class compiler creates nested class with unspeakable name, lets name it X for simplicity, and puts all local variables there. Also it writes lambda there, so it becomes normal method. Then compiler rewrites your method so that it creates X at some point and replaces access to machine, info and newConfigPath with x.machine, x.info and x.newConfigPath respectively. Also X receives reference to this, so lambda-method could access transId via parentRef.transId.
Well, it is extremely simplified but near to reality.
UPD:
class A
{
private int b;
private int Call(int m, int n)
{
return m + n;
}
private void Method()
{
int a = 5;
a += 5;
Func<int> lambda = () => Call(a, b);
Console.WriteLine(lambda());
}
#region compiler rewrites Method to RewrittenMethod and adds nested class X
private class X
{
private readonly A _parentRef;
public int a;
public X(A parentRef)
{
_parentRef = parentRef;
}
public int Lambda()
{
return _parentRef.Call(a, _parentRef.b);
}
}
private void RewrittenMethod()
{
X x = new X(this);
x.a += 5;
Console.WriteLine(x.Lambda());
}
#endregion
}

Share data between threads

I'm trying to implement an algorithm that should run in parallel using threads or tasks. The difficulty is that I want the threads/tasks to share their best results from time to time with all other threads.
The basic idea is this:
//Accessible from each thread
IProducerConsumerCollection<MyObject> _bestObjects;
//Executed in each thread
DoSomeWork(int n){
MyObject localObject;
for(var i = 0; i < n; i++){
//Do some calculations and store results in localObject
if((i/n)%0.5 == 0)
{
//store localObject in _bestObjects
//wait until each thread has stored its result in _bestObjects
//get the best result from _bestObjects and go on
}
}
}
How can this be achieved using System.Threading or System.Threading.Tasks and is it true that tasks should not be used for long running operations?
Update: Clarification
It's not my problem to have a thread safe collection but to make the threads stop, publish result, wait until all other threads have publihed their results to and then go on again. All threads will run simultaneously.
Cutting a long story short:
Whats better for long running operations? Task or Thread or anything else?
How to communicate between threads/taks to inform each of them about the state of all other assuming that the number of threads is set at runtime (depending on available cores).
Best Regards
Jay
Look at the dollowing example.
public class Worker
{
public SharedData state;
public void Work(SharedData someData)
{
this.state = someData;
while (true) ;
}
}
public class SharedData {
X myX;
public getX() { ... }
public setX(anX) { ... }
}
public class Sharing
{
public static void Main()
{
SharedData data = new SharedDate()
Worker work1 = new Worker(data);
Worker work2 = new Worker(data);
Thread thread = new Thread(new ThreadStart(work1.Work));
thread.start();
Thread thread2 = new Thread(new ThreadStart(work2.Work));
thread2.start();
}
}
bomslang's response is not accurate. Cannot instantiate a new thread with ThreadStart, passing in Work method which requires a parameter to be passed in the above example. ParameterizedThreadStart would be more suitable. The sample code for the Main method would look more like this:
public class Sharing
{
public static void Main()
{
SharedData data = new SharedDate()
Worker work1 = new Worker(data);
Worker work2 = new Worker(data);
Thread thread = new Thread(new ParameterizedThreadStart(work1.Work));
thread.start(someData);
Thread thread2 = new Thread(new ParameterizedThreadStart(work2.Work));
thread2.start(someData);
}
}
Note that 'work' is being passed into the ParameterizedThreadStart as the method for the new thread to execute, and the data required to pass in to the 'work' method is being passed in the call to start. The data must be passed as an object, so the work method will need to cast it back to the appropriate datatype as well. Lastly, there is also another approach to passing in data to a new thread via the use of anonymous methods.

Categories