The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

octavio08 (8) [Avatar] Offline
#1
Hi

I ran the example but the code worked as expected (in theory), it printed the numbers 1 to 10.

Closure can lead to some strange behaviors. In theory, this program should work: we are
expecting the program to print the numbers 1 to 10. However, in practice, this is not the case;
the program will print the number 10 ten times
.


I modified the code to move the Task.Factory.StartNew inside the FOR loop. With this change, the code printed the number 11 ten times.

for (int i = 1; i <= iterations; i++)
{
Task.Factory.StartNew(() => Console.WriteLine("{0} - {1}", Thread.CurrentThread.ManagedThreadId, i));
}
Riccardo Terrell (31) [Avatar] Offline
#2
I have run the code using .NET framework 3.5, 4.0 and 4.5. I am getting back the result as expected, which is the same last value assigned to the variable i for all the iterations.
I used the value 11 for the iterations variable, and I am getting 11 times the value 11.
here the code

            int iterations = 10;
    
            for (int i = 1; i <= iterations; i++)
            {
                Task.Factory.StartNew(() =>
                    Console.WriteLine("{0} - {1}", Thread.CurrentThread.ManagedThreadId, i));
            }


I have attached an image too.

Which .NET framework are you using?
octavio08 (8) [Avatar] Offline
#3
Listing 2.8
[ 33 KB ]
Output 1
[ 16 KB ]
Hi

I ran the code of the listing 2.8 (page 37):

int iterations = 10;
Task.Factory.StartNew(() => Loop(iterations));

static void Loop(int iterations)
{
    for (int i = 1; i <= iterations; i++){
        Console.WriteLine("{0} - {1}", Thread.CurrentThread.ManagedThreadId, i);
    }
}


I attached the output (Output 1).

I am using .NET Framework 4.6.1.

Riccardo Terrell (31) [Avatar] Offline
#4
Thanks for the reply. Yes, you are correct then.
This is an issue in the code, which as you can see there is not closure but it is just a conventional for loop

for (int i = 1; i <= iterations; i++){
        Console.WriteLine("{0} - {1}", Thread.CurrentThread.ManagedThreadId, i);
    }


in this case the code run in a single thread. However, in the code mentioned in my answer, the loop has a closure over a Task inside the for loop.
for (int i = 1; i <= iterations; i++){
    Task.Factory.StartNew(() =>
        Console.WriteLine("{0} - {1}", Thread.CurrentThread.ManagedThreadId, i));


This code has been fixed and it will be publish for download soon.
In the next MEAP, which is scheduled by the end of the month, the source code will be corrected and available to download. Thus, you will be able to open the solution and run the code
octavio08 (8) [Avatar] Offline
#5
Thanks!!