1

Can anyone explain the below code, especially this line: starter += () => {. I'm not sure how it functions and how the whole code works.

private void RunNetworkCheckThread()
{
    ThreadStart starter = CheckNetwork;

    starter += () =>
    {
        if (!_withNetwork)
        {
            RunNetworkCheckThread();
        }
        else
        {
            StartDrive();                   
        }
    };

    threadNetwork = new Thread(starter) { IsBackground = true }; 
    threadNetwork.Start();
}
Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
yonan2236
  • 13,371
  • 33
  • 95
  • 141
  • @juharr: Well without that, in this case `starter` would only execute either `CheckNetwork` or the lambda expression, not both... – Jon Skeet Sep 06 '18 at 14:31
  • 2
    It's syntactic sugar for `starter = (ThreadStart) Delegate.Combine(starter, new ThreadStart(delegate { ... }));` -- but that may not clear things up much if you don't know how [delegates](https://learn.microsoft.com/dotnet/csharp/programming-guide/delegates/) work. – Jeroen Mostert Sep 06 '18 at 14:32
  • @DaisyShipton Yeah, just noticed that `starter` was being assigned `CheckNetwork` first. – juharr Sep 06 '18 at 14:33
  • Incidentally, this code is needlessly convoluted. `starter = () => { CheckNetwork(); ... the rest... }` is not formally the same, but has the same effect without the multicast delegate. As written, this code propagates the dangerous notion that creating multicast delegates is a good way of chaining blocks of code together, which is really not the case. The author may also have labored under the confusion that these different blocks of code would execute in parallel -- which is also not the case. – Jeroen Mostert Sep 06 '18 at 14:43
  • Possible duplicate of [Meaning of () => Operator in C#, if it exists](https://stackoverflow.com/questions/3627840/meaning-of-operator-in-c-if-it-exists) – Chrᴉz remembers Monica Sep 06 '18 at 15:01
  • @JeroenMostert so with the above code, is it the CheckNetwork method which gets executed first? – yonan2236 Sep 06 '18 at 15:17

3 Answers3

3

The ThreadStart is a delegate type wich can hold many functions/methods. the symbol ()=>{} is used to define a method wich is added to your delegate by the operator +=

gandalf
  • 451
  • 5
  • 18
  • Is there a longer version/implementation of this? Something more readable? – yonan2236 Sep 06 '18 at 14:33
  • something like `void doSomething(){ ... }` `Thread thr = new Thread(doSomething); thr.start();` maybe? – cagri Sep 06 '18 at 14:35
  • @yonan2236: It's difficult to help when we don't know much of this you already understand. Do you know about threads? Delegates? Lambda expressions? If you're not already aware of lambda expressions, I'd recommend getting a good book for a detailed explanation. Stack Overflow is great for many things, but it's not the ideal way of learning meaty topics like lambdas. – Jon Skeet Sep 06 '18 at 14:35
3
  1. ThreadStart is a delegate.
  2. ()=>{} is a lambda (can be used to create a delegate)
  3. += Delegates can be combined together. ThreadStarter was assigned to CheckNetwork delegate (when you call ThreadStarter you call CheckNetwork). But then to ThreadStarter was added a delegate created from anonymous function (lambda function). So now when you call ThreadStarter CheckNetwork will execute and then lambda function will execute.

More easy to read:

private void RunNetworkCheckThread(){
        ThreadStart starter =  () =>
        {
           CheckNetwork();

           if (_withNetwork) StartDrive();
           else RunNetworkCheckThread();
        };

        threadNetwork = new Thread(starter) { IsBackground = true };
        threadNetwork.Start();
}

PS More info on Combining delegates is provided here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/how-to-combine-delegates-multicast-delegates

JohnIdlewood
  • 356
  • 3
  • 12
3

ThreadStart starter is delegate. CheckNetwork is a target method. the following is the target method as well

() =>
    {
        if (!_withNetwork)
        {
            RunNetworkCheckThread();
        }
        else
        {
            StartDrive();                   
        }
    };

starter += () - this is what makes starter multicast delegate How to: Combine Delegates

this line threadNetwork.Start() call two delegate in the same thread. so CheckNetwork and Action method are being executed in the same new thread.

Z.R.T.
  • 1,543
  • 12
  • 15