pglazkov / MvvmValidation

Lightweight library that helps reduce boilerplate when implementing validation in XAML MVVM applications
MIT License
167 stars 31 forks source link

ResultChanged event is called on a wrong thread (non-UI) #11

Closed pglazkov closed 7 years ago

pglazkov commented 7 years ago

ResultChanged event is called on a wrong thread (non-UI) when synchronous validation is executed (ValidationHelper.Validate(...)).

    ===== Main exception =====
    MvvmValidation.ValidationException: An exception occurred during validation. See inner exception for details. ---> System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
   at System.Windows.Threading.Dispatcher.VerifyAccess()
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at XXX.Views.Main.ProjectDataV.set_ComputeVolumePreReqOK(Boolean value) in c:\Users\xga\Dev\geonX\Virfac-AM\Virfac-iAM\Views\Main\ProjectDataV.xaml.cs:line 44
   at XXX.Views.Main.ProjectDataV.UpdatePreReqs(Object sender, PropertyChangedEventArgs e) in c:\Users\xga\Dev\geonX\Virfac-AM\Virfac-iAM\Views\Main\ProjectDataV.xaml.cs:line 125
   at System.Windows.WeakEventManager.ListenerList`1.DeliverEvent(Object sender, EventArgs e, Type managerType)
   at System.ComponentModel.PropertyChangedEventManager.OnPropertyChanged(Object sender, PropertyChangedEventArgs args)
   at GalaSoft.MvvmLight.ObservableObject.RaisePropertyChanged(String propertyName)
   at YYYLib.Mvvm.ViewModels.MvvmVM.set_IsValid(Boolean value) in c:\Users\xga\Dev\geonX\Virfac-AM\YYYLib.Mvvm\ViewModels\MvvmVM.cs:line 152
   at YYYLib.Mvvm.ViewModels.MvvmVM.CheckAndUpdateValidity() in c:\Users\xga\Dev\geonX\Virfac-AM\YYYLib.Mvvm\ViewModels\MvvmVM.cs:line 317
   at YYYLib.Mvvm.ViewModels.MvvmVM.OnValidationResultChanged(Object sender, ValidationResultChangedEventArgs e) in c:\Users\xga\Dev\geonX\Virfac-AM\YYYLib.Mvvm\ViewModels\MvvmVM.cs:line 297
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at MvvmValidation.ValidationHelper.NotifyResultChanged(Object target, ValidationResult newResult, SynchronizationContext syncContext, Boolean useSyncContext) in E:\Pasha\Dev\MyProjects\MvvmValidation\MvvmValidation.Portable\ValidationHelper.cs:line 999
   at MvvmValidation.ValidationHelper.SaveRuleValidationResultAndNotifyIfNeeded(ValidationRule rule, RuleResult ruleResult, SynchronizationContext syncContext) in E:\Pasha\Dev\MyProjects\MvvmValidation\MvvmValidation.Portable\ValidationHelper.cs:line 840
   at MvvmValidation.ValidationHelper.<>c__DisplayClass46_0.<ExecuteNextRuleFromQueueAsync>b__0(Task`1 t) in E:\Pasha\Dev\MyProjects\MvvmValidation\MvvmValidation.Portable\ValidationHelper.cs:line 759
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
   --- End of inner exception stack trace ---
   at MvvmValidation.ValidationHelper.ValidateInternal(Object target) in E:\Pasha\Dev\MyProjects\MvvmValidation\MvvmValidation.Portable\ValidationHelper.cs:line 686
   at MvvmValidation.ValidationHelper.Validate(String targetName) in E:\Pasha\Dev\MyProjects\MvvmValidation\MvvmValidation.Portable\ValidationHelper.cs:line 607
   at YYYLib.WorkItems.ViewModels.WorkItemsManagerVM.set_IsEmptyItemBounds(Boolean value) in c:\Users\xga\Dev\geonX\Virfac-AM\YYYLib.WorkItems\ViewModels\WorkItemsManagerVM.cs:line 68
    ===== Inner exception =====
    System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
   at System.Windows.Threading.Dispatcher.VerifyAccess()
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at XXX.Views.Main.ProjectDataV.set_ComputeVolumePreReqOK(Boolean value) in c:\Users\xga\Dev\geonX\Virfac-AM\Virfac-iAM\Views\Main\ProjectDataV.xaml.cs:line 44
   at XXX.Views.Main.ProjectDataV.UpdatePreReqs(Object sender, PropertyChangedEventArgs e) in c:\Users\xga\Dev\geonX\Virfac-AM\Virfac-iAM\Views\Main\ProjectDataV.xaml.cs:line 125
   at System.Windows.WeakEventManager.ListenerList`1.DeliverEvent(Object sender, EventArgs e, Type managerType)
   at System.ComponentModel.PropertyChangedEventManager.OnPropertyChanged(Object sender, PropertyChangedEventArgs args)
   at GalaSoft.MvvmLight.ObservableObject.RaisePropertyChanged(String propertyName)
   at YYYLib.Mvvm.ViewModels.MvvmVM.set_IsValid(Boolean value) in c:\Users\xga\Dev\geonX\Virfac-AM\YYYLib.Mvvm\ViewModels\MvvmVM.cs:line 152
   at YYYLib.Mvvm.ViewModels.MvvmVM.CheckAndUpdateValidity() in c:\Users\xga\Dev\geonX\Virfac-AM\YYYLib.Mvvm\ViewModels\MvvmVM.cs:line 317
   at YYYLib.Mvvm.ViewModels.MvvmVM.OnValidationResultChanged(Object sender, ValidationResultChangedEventArgs e) in c:\Users\xga\Dev\geonX\Virfac-AM\YYYLib.Mvvm\ViewModels\MvvmVM.cs:line 297
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at MvvmValidation.ValidationHelper.NotifyResultChanged(Object target, ValidationResult newResult, SynchronizationContext syncContext, Boolean useSyncContext) in E:\Pasha\Dev\MyProjects\MvvmValidation\MvvmValidation.Portable\ValidationHelper.cs:line 999
   at MvvmValidation.ValidationHelper.SaveRuleValidationResultAndNotifyIfNeeded(ValidationRule rule, RuleResult ruleResult, SynchronizationContext syncContext) in E:\Pasha\Dev\MyProjects\MvvmValidation\MvvmValidation.Portable\ValidationHelper.cs:line 840
   at MvvmValidation.ValidationHelper.<>c__DisplayClass46_0.<ExecuteNextRuleFromQueueAsync>b__0(Task`1 t) in E:\Pasha\Dev\MyProjects\MvvmValidation\MvvmValidation.Portable\ValidationHelper.cs:line 759
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()