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
}
Related
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.
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 follow Passing Data to a Thread of "PART 1: GETTING STARTED" of "Threading in C#" by Joseph Albahari.
Namely the passage:
====== Start of quote
"With this approach, you can pass in (to where?) any number of arguments to the method. You can even wrap the entire implementation in a multi-statement lambda:
new Thread (() =>
{
Console.WriteLine ("I'm running on another thread!");
Console.WriteLine ("This is so easy!");
}).Start();*
You can do the same thing almost as easily in C# 2.0 with anonymous methods:
new Thread (delegate()
{
...
}).Start();
============ End of quote
That is, I've tried the "easily" as:
new Thread
(delegate
{
Console.WriteLine("I'm running on another thread!");
Console.WriteLine("This is so easy!");
}
).Start();
but it produces the error:
The call is ambiguous between the following methods or properties:
'System.Threading.Thread.Thread(System.Threading.ThreadStart)' and
'System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart)'
How can you disambiguate the code in order to run it? Answered (missed parenthesis. Anyway, it wasn't the original main question)
Also, I did not quite grasp where is the empty list () => directed/applied to?
A well as, what is the method to which " you can pass in any number of arguments to the method"?
How to understand the passing of (any number of) arguments through the empty list?
Update (addressing Jon Skeet's comment):
No, I am not stuck with C# 2.
The same question(s) to the previous passage:
========== Start of quote:
"The easiest way to pass arguments to a thread’s target method is to execute a lambda expression that calls the method with the desired arguments:
static void Main()
{
Thread t = new Thread ( () => Print ("Hello from t!") );
t.Start();
}
static void Print (string message)
{
Console.WriteLine (message);
}
With this approach, you can pass in any number of arguments to the method."
=============== End of quote
Update2:
The most complete answer is IMO by #Lee though I marked as correct another answer for guessing to answer at once what I have not even originally asked - how to put something in empty parenthesis (I'm already afraid to call it by list or by arguments)
You need to make the argument list explicit:
new Thread
(delegate()
{
Console.WriteLine("I'm running on another thread!");
Console.WriteLine("This is so easy!");
}
).Start();
The delegate keyword allows you to define an anonymous method. Lambda expressions (i.e. using the () => { ... } syntax) are similar, however using delegate allows you to omit the parameter list. This is ambiguous in this case, since there are two constructors for Thread which take different delegate types. One takes a ThreadStart which is defined as
delegate void ThreadStart();
and the other takes a ParameterizedThreadStart which is defined as:
delegate void ParameterizedThreadStart(object state);
Since you are omitting the parameter list, the compiler does not know which delegate type you are using.
I assume the "any number of arguments" are variables which are closed-over by your delegate. For example you could have:
string message = "This is so easy!";
var thread = new Thread(delegate() {
Console.WriteLine(message);
});
thread.Start();
You can use a ParameterizedThreadStart to pass an arbitrary object to your thread delegate e.g.
public class ThreadData {
//properties to pass to thread
}
ThreadData data = new ThreadData { ... }
Thread thread = new Thread((object state) => {
ThreadData data = (ThreadData)state;
});
thread.Start(data);
You need the parentheses after delegate to specify the parameters, in this case no parameters:
new Thread(
delegate() {
Console.WriteLine("I'm running on another thread!");
Console.WriteLine("This is so easy!");
}
).Start();
The "any number of arguments" that the author is talking about is that you can use data from the scope where the delegate is created inside the code that runs in a separate thread, without having to pass the data to the Start method:
string msg1 = "I'm running on another thread!";
string msg2 = "This is so easy!";
new Thread(
delegate() {
Console.WriteLine(msg1);
Console.WriteLine(msg2);
}
).Start();
What's actually happening is that the variables are no longer local variables in the method, instead they are automatically stored in a closure, which the delegate shares with the method where it is defined.
This works well as long as you only want to start one thread. If you want to start multiple threads that uses different data, you would either pass the data into the Start method or create a class that can hold the data, put the code for the thread in the class, and create one instance for each thread that you start.
In order to resolve ambiguous call, you can add empty parentheses (delegate will be treated as ThreadStart delegate):
new Thread(delegate() {
Console.WriteLine("I'm running on another thread!");
Console.WriteLine("This is so easy!");
}).Start();
Or add state parameter (delegate will be treated as ParametrizedThreadStart) It works, but it's odd, because you don't need that parameter.
new Thread(delegate(object state) {
Console.WriteLine("I'm running on another thread!");
Console.WriteLine("This is so easy!");
}).Start();
Or cast delegate to ThreadStart
new Thread((ThreadStart)delegate {
Console.WriteLine("I'm running on another thread!");
Console.WriteLine("This is so easy!");
}).Start();
() => isn't an empty list in C#. In the context it is used in the book it is the start of a lambda expression. The () means that this expression takes no arguments.
How can I pass a Method2 to thread as shown in below if i write code as shown in below it shows an error Error
The best overloaded method match for
'System.Threading.Thread.Thread(System.Threading.ThreadStart)' has
some invalid arguments
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main Thread : ");
Thread obj = new Thread(Method2);
obj.Start();
Console.ReadLine();
}
private static int Method2(int a)
{
return a;
}
}
When i use the following code it works
Thread obj = new Thread(() => Method2(1));
But why it's not working when i pass delegate object
delegate int del(int i);
del d = Method2;
Thread obj = new Thread(d);
What is the difference between above 2 , in 1st case i used Lambda expression in second case directly passed delegate object is there any thing else?
The Thread constuctor takes two types of delegates, ThreadStart and ParameterizedThreadStart. To see what type of method these delegates accept you can create it and the constructor will show you, so for example,
var parameterMethod = new ParameterizedThreadStart(...
If you type the above you will see the delegate takes a function with one object as the parameter and returns void.
void (object) target
This is why your Method that takes an int will not work because the signature does not match the delegate target signature. When you wrap it in a lambada, you are actually passing in a method that takes no parameters and returns void, so the Method2 signature is not even looked at. Wrapping method calls with a lambada like this can be very useful for getting the signature to match.
delegate void del(object obj);
del d = () => Method2(1);
Thread obj = new Thread(d);
There is an infinite number of possible method signatures so Threads and Tasks keep it simple and say if you want to pass something, pass a single object. Since all types derive from object, this lets you pass in anything you want, then cast it later.
Try this:
Thread obj = new Thread(() => Method2(some_int_value));
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).