tutu tutu - 1 month ago 16
C# Question

understanding threads why count not increase

I don't unrdersntat why with this code

static void Main(string[] args)
{
Console.WriteLine("START PROGRAMN-----------------------------------");
test();
Console.WriteLine("END PROGRAMN-----------------------------------");
Console.Read();
}
[ThreadStatic]
private static int i;
private static void test()
{
for (i = 0; i < 2; i++)
{
var bw = new BackgroundWorker();

// define the event handlers
bw.DoWork += (sender, args) =>
{
Console.WriteLine("START Thread-------------");
Console.WriteLine("Print:" + i);
};
bw.RunWorkerCompleted += (sender, args) =>
{
Console.WriteLine("END Thread-------------");
if (args.Error != null)
{
Console.WriteLine(args.Error.ToString());
}
};

bw.RunWorkerAsync(); // starts the
}
}


It will show this in console:

START PROGRAMN-----------------------------------
END PROGRAMN-----------------------------------
START Thread-------------
Print:0
END Thread-------------
START Thread-------------
Print:0
END Thread-------------


Why second print doesn't show print 1?

I think the first iteration is correct because I see print: 0 but in second why I don't see print: 1?

EDIT FOR ANSER

Without [ThreadStatic]

static void Main(string[] args)
{
Console.WriteLine("START PROGRAMN-----------------------------------");
test();
Console.WriteLine("END PROGRAMN-----------------------------------");
Console.Read();
}
[ThreadStatic]
private static int i;
private static void test()
{
for (i = 0; i < 2; i++)
{
var bw = new BackgroundWorker();

// define the event handlers
bw.DoWork += (sender, args) =>
{
Console.WriteLine("START Thread-------------");
Console.WriteLine("Print:" + i);
};
bw.RunWorkerCompleted += (sender, args) =>
{
Console.WriteLine("END Thread-------------");
if (args.Error != null)
{
Console.WriteLine(args.Error.ToString());
}
};

bw.RunWorkerAsync(); // starts the
}
}


It will show this in console:

START PROGRAMN-----------------------------------
END PROGRAMN-----------------------------------
START Thread-------------
Print:2
END Thread-------------
START Thread-------------
Print:2
END Thread-------------


Why first pirnt doesn't show print 0 and second print 1?

why show 2?

Answer

Forget about that ThreadStatic you are not using this properly. It's not what you need in you case. The problem you are trying to bypass is because otherwise your result gets the latest value because the DoEvent is not started on the first thread that it already changed. You end up with a race condition. You do need to use arguments to have clear local instance of the variable. the easiest way it to change your code like so

for (int i = 0; i < 2; i++)
{
     var bw = new BackgroundWorker();

     // define the event handlers
     bw.DoWork += (sender, args) =>
     {
          // get the argument
          var value = args.Argument.ToString();
          Console.WriteLine("START Thread-------------");
          Console.WriteLine("Print:" + value);
     };
     bw.RunWorkerCompleted += (sender, args) =>
     {
         Console.WriteLine("END Thread-------------");
         if (args.Error != null)
         {
             Console.WriteLine(args.Error.ToString());
         }
     };

     bw.RunWorkerAsync(i); // starts the thread with arguments
 }