I have a property in my ViewModel class
private bool _isMonitoring;
// etc.
public bool IsMonitoring
{
get { return _isMonitoring; }
set
{
_isMonitoring = value;
OnPropertyChanged("IsMonitoring"); // MVVM notify property change using PropertyChangedEventHandler.
}
}
... and a method that sets this value.
public void StopMonitoring()
{
if (IsMonitoring)
{
IsMonitoring = false;
Logger.Instance.LogDebug("Stopping the real time test.");
_myController.StopRealTimeTest();
}
}
...That StopRealTimeTest() method fires an event...
public void StopRealTimeTest()
{
// Other unrelated code
OnMeasurementEnded();
}
private void OnMeasurementEnded(RealTimeFrameTranslator updateInfo = null, bool connectionError = false)
{
if (MeasurementEnded != null)
{
// Other code to set eventArgs
MeasurementEnded(this, eventArgs);
}
}
... which has a handler in the original ViewModel class.
private void MyController_MeasurementEnded(object sender, MyMeasurementEventArgs e)
{
if (IsMonitoring)
{
_myController.GetRealTimeTest(new System.Threading.CancellationToken());
}
}
Now I got a report of an exception, and according to the stack trace (which I got from a log of the exception), it occurred in the GetRealTimeTest() method, and looking down through the stack trace, it went through the methods listed above to get there. But I don't understand how it got to GetRealTimeTest() because the IsMonitoring property would have been set to false before the event is fired.
I can't reproduce this situation myself, but is it possible at all that there is some quirk with event handlers (or MVVM type properties), e.g. the event handler could complete before the property is completely updated, or that it would not regard the latest value of it for some reason or something else?
I realise there may be a number of ways to fix this, but I need to be able to explain what went wrong, hopefully with a view to reproducing it.