Is it a good practice to have a static variable as counter that can be updated by all the threads in a C#.NET program?
Sample Code:
public class SomeTask
{
static int count = 0;
public void Process()
{
while(true)
{
//some repeated task
count++;
if(count>100000)
{
count=0;
break;
}
}
}
}
public class WorkerRole : RoleEntryPoint
{
public override void Run()
{
while(true)
{
for (int i = 0; i < maxTasks; i++)
{
this.Tasks[i] = Task.Factory.StartNew(() => (new SomeTask()).Process());
}
Task.WaitAll(this.Tasks);
//every 100000 in counter needs some updates at program level
}
}
}