This is probably due to stream optimization. To safely “see” a change in iswaiting in release mode, you need a protective memory barrier.
The easiest way to “fix” this would mean iswaiting as volatile :
volatile bool iswaiting;
Speaking of this, “rotation” like this will completely absorb one CPU core. It would be much better to use ManualResetEvent to signal that you can continue.
// Add: private ManualResetEvent allowProgress = new ManualResetEvent(false);
Then, instead of using iswaiting, you should:
_bg.ReportProgress(1, filePath); allowProgress.WaitOne();
To continue, use:
result = Microsoft.Windows.Controls.MessageBox.Show("Question" ,"Title", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning); allowProgress.Set();
The advantage is that you will not use the processor during the lock, and you do not need to worry about the memory barriers themselves.
source share