Task Foo(IProgress<int> onProgressPercentChanged){
return Task.Run(() =>{
for (int i = 0; i < 1000; i++){
if (i % 10 == 0)
onProgressPercentChanged.Report(i / 10);
//Some operation
}
});
}
var progress = new Progress<int>(i => Console.WriteLine(i + " %"));
await Foo(progress);
Thread.Sleep(10000);
The above code prints my progress reports in the incorrect order. I suspected that this issue was related to synchronization, as when I add Thread.Sleep(10)
at the //Some operation
place, everything starts working correctly. How can I achieve correctness without the unnecessary Thread.Sleep(10)
?