Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please find below code snippet and help to resolve it.

Program.cs:
C#
class Program
   {
       static void Main(string[] args)
       {
           for (int i = 0; i < 5; i++)
           {
               Class1 obj = new Class1();
               new Thread(() => obj.Show(i)).Start();
           }

           Console.ReadLine();
       }
   }


Class1:
C#
class Class1
    {
        public void Show(int i)
        {
            Console.WriteLine("Show: " + i);
            Thread.Sleep(i * 100);
        }
    }


Excepted Output:
Show: 0
Show: 1
Show: 2
Show: 3
Show: 4

But due to threading i am not getting result in above way
the result is unpredictable like
Show: 3
Show: 3
Show: 3
Show: 5
Show: 4

Can any one let me know, how to resolve this kind of issue?
Posted

You have captured the loop variable in a closure. That doesn't work as you'd expect:
Closing over the loop variable considered harmful : Eric Lippert’s Blog[^]

In .NET 4.5, this was fixed for foreach loops; however, for loops retain the old behaviour.

To fix it, you need to create a copy of the loop variable, and use that copy in your closure:
C#
for (int i = 0; i < 5; i++)
{
    int iCopy = i;
    Class1 obj = new Class1();
    new Thread(() => obj.Show(iCopy)).Start();
}
 
Share this answer
 
Comments
indyarock 21-May-15 9:40am    
it fixed :)
Thank you
Pankit Patel 22-May-15 0:15am    
this will print 0 to 5 but not wait for output...as you mention Thread.Sleep()
Richard Deeming 22-May-15 8:10am    
If you look at the question, you'll see that the Main method has a Console.ReadLine(); after this loop. The program will keep running until the user presses the "Enter" key, giving the threads time to complete.
Pankit Patel 22-May-15 8:27am    
but when u run this console application it will print 0 to 4 immediate .It will not wait for a second as per statement ( i * 100).
and Console.ReadLine() will always wait for User input then after it will exit.
But main matter is about Threading.
Richard Deeming 22-May-15 8:30am    
If you read the original question, you'll see that this is what the OP wanted.

The question is not "how do I get the values to print after the call to Thread.Sleep". The question is "how do I get the correct values to print".
I have tried this and working as per your expecteation.
for (int i = 1; i <= 10; i++)
{
    Class1 obj = new Class1();
    obj.Show(i);
    //new Thread(() => obj.Show(i)).Start();
}
Console.ReadLine();
 
Share this answer
 
Comments
indyarock 21-May-15 9:37am    
i need it with threading!!
Try to change the method Show to this.
C#
public void Show(int i)
{
    Console.WriteLine("Show: " + i);
    Thread.Sleep(i * 100);
    Console.WriteLine("Show: " + i);
}

It makes more sense to put the sleep before the print out.
 
Share this answer
 
v2
Comments
indyarock 21-May-15 9:36am    
No, i didn't solve :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900