I would like to make my getter thread safe. When I do this, I get an error:
public ApplicationViewModel SelectedApplication
{
get
{
if (InvokeRequired)
{
BeginInvoke((Action<ApplicationViewModel>)SelectedApplication);
}
return _applicationsCombobox.SelectedItem as ApplicationViewModel;
}
}
I have the error:
Cannot cast expression of type 'Foo.Model.ApplicationViewModel' to type 'Action<ApplicationViewModel>'
BeginInvoke
takes a delegate.You can write
Lots of things wrong:
Invoke
is requiredFunc<>
is requiredelse
is required.Which produces:
Hiding the thread context switches in a low-level property is usually a mistake. Invoking has lots of overhead, the resulting code may end up being very slow without a good hint of why it is so slow.
Try: