Send data using threads - c#

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

Related

How to convert thread Code (VB.net to C#)..?

how to convert my VB.net code to C#..?
I want to use a looping function with a thread.
VB.Net Code
For i As Integer = 0 To _port_count
Dim worker As New Threading.Thread(AddressOf looping)
worker.Start(_list(i))
commThread.Add(worker)
Next
public sub looping(Byvar PortList As PortList) 'looping function
C# Code
for (int i = 0; i <= _port_count; i++)
{
Thread worker = new Thread(looping);
worker.Start(_list[i]);
commThread.Add(worker);
}
public static void looping (PortList PortList) {}
but C# code didn't work. :(
thanks for your helps.
The Thread constructor will take either a ThreadStart delegate or a ParameterizedThreadStart delegate. The first has no parameters and the second has one parameter of type object. If you're using a named method then it has to match one of those two signatures, which yours does not. VB will allow you to do the wrong thing and attempt to clean up for you if it can but C# expects you to do the right thing yourself. These days, if you want to call a method whose signature does not match, you can use a Lambda with a matching signature instead, then call your method in that:
Thread worker = new Thread(() => looping(_list[i]));
worker.Start();
What's declaration of looping function ?
And some more info ?
Here is an example from Microsoft Docs.
using System;
using System.Threading;
public class Work
{
public static void Main()
{
// Start a thread that calls a parameterized static method.
Thread newThread = new Thread(Work.DoWork);
newThread.Start(42);
// Start a thread that calls a parameterized instance method.
Work w = new Work();
newThread = new Thread(w.DoMoreWork);
newThread.Start("The answer.");
}
public static void DoWork(object data)
{
Console.WriteLine("Static thread procedure. Data='{0}'",
data);
}
public void DoMoreWork(object data)
{
Console.WriteLine("Instance thread procedure. Data='{0}'",
data);
}
}
// This example displays output like the following:
// Static thread procedure. Data='42'
// Instance thread procedure. Data='The answer.'
link: https://msdn.microsoft.com/en-us/library/1h2f2459(v=vs.110)
Change you looping-method signature to following:
public static void looping (object PortList)
As documentation says, ParameterizedThreadStart passed as Thread constructor parameter has to be object.

Running a paramaterised method in a thread

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

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.

What do i create at thread with a function with parameters?

What do i create at thread with a function with parameters?
Thread t = new Thread(new ParameterizedThreadStart(fetchQuotes(cp)));
void fetchQuotes(SomeObject obj)
{
[DoSomething With SomeObject]
}
The easiest approach here is to use a closure over the non-parameterised (ThreadStart) signature:
Thread t = new Thread(() => fetchQuotes(cp));
...
t.Start();
This is static-checked for correctness at compile-time, and convenient (you can pass any number of parameters, for example).
The other approach is to pass object as the parameter (ParameterizedThreadStart):
Thread t = new Thread(fetchQuotes);
...
t.Start(cp);
...
void fetchQuotes(object obj)
{
SomeObject obj2 = (SomeObject) obj;
[DoSomething With SomeObject]
}
here we are passing object, so not type-checked at compile time. "braindead" errors will only surface at runtime.
I'll add the TPL syntax here for completeness (see my previous comment).
Task task = new Task(() => FetchQuotes(arg));
task.Start();
The task library has plenty of options for running on the calling thread, or async on one or many threads etc.
http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx

Categories