hello,
my view model creates a BackgroundWorker in its constructor. BackgroundWorker updates the model's properties from its DoWork event handler. The code below is a contrived example (ViewModelBase is taken almost verbatim from the MVVM paper).
public class MyViewModel : ViewModelBase
{
public int MyProperty
{
get
{
return this.my_property;
}
private set
{
if (this.my_property != value)
{
this.my_property = value;
this.OnPropertyChanged("MyProperty");
}
}
}
public MyViewModel()
{
this.worker = new BackgroundWorker();
this.worker.DoWork += (s, e) => { this.MyProperty = 1; };
this.worker.RunWorkerAsync();
}
}
my view is bound to this view model:
public partial class MyPage : Page
{
public MyPage()
{
InitializeComponent();
this.DataContext = new MyViewModel();
}
}
the problem is that my application crashes intermittently when my page is displayed. this definitely has something to do with the worker thread and data binding in XAML. it seems that if i start the worker from inside the Loaded event handler for the page, the problem goes away but since it is hard to reproduce consistently, i am not sure whether this is the right fix.
does any one have any suggestions or ideas what might be the exact cause?
EDIT: i only can reproduce it if i run it without a debugger. and the error in the system log is InvalidOperationException thrown from ShowDialog, which is not much help. under the debugger it runs fine.
thanks for any help konstantin