I implemented a sample application to see the behavior of the retries inside a parallel foreach loop. As per my observations following application not showing all the values inside the array ,in the textbox (Showing character number changes randomly).
I want to know what cause this behavior. Thanks
public string DisplayText = "Numbers =";
private void button1_Click(object sender, EventArgs e)
{
int[] numbers = { 1, 2, 3,4,5,6,7,8,9};
Start(numbers);
textBox1.Text = DisplayText;
}
private void Start(int[] numbers)
{
Parallel.ForEach(numbers, k =>
{
Write(k.ToString());
});
}
private void Write(string text)
{
int maxRetryCount = 3;
int retryCount = 0;
int retryInterval = 1000;
while (retryCount < maxRetryCount)
{
if(retryCount == maxRetryCount-1)
{
DisplayText = DisplayText + "," + text;
break;
}
Thread.Sleep(retryInterval);
retryCount++;
}
}
4