Would changing a session variable (i.e. Session["Progress"]) in code below be safe?
This code is part of code-behind of an ASP.Net page.
When running a loop in parallel, two iterations could run simultaneously and cause issues if the same session variable is changed by both iterations.
public void LongOperation()
{
Parallel.For(0, totalMembers,(i,loopState) =>
{
Thread.Sleep(2000);//wait some time to simulate processing
progress++;
Session["Progress"] = progress;//IS THIS THREAD-SAFE?
}
);
}