C# InvokeRequired with property getter

2019-07-18 10:50发布

问题:

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>'

回答1:

Lots of things wrong:

  • you cannot use BeginInvoke, Invoke is required
  • you cannot use Action<>, you are returning a value so Func<> is required
  • you cannot also run the original code after invoking, else is required.

Which produces:

public ApplicationViewModel SelectedApplication
{
    get
    {
        if (this.InvokeRequired)
            return (ApplicationViewModel)this.Invoke(new Func<ApplicationViewModel>(() => this.SelectedApplication));
        else
            return _applicationsCombobox.SelectedItem as ApplicationViewModel;
    }
}

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.



回答2:

BeginInvoke takes a delegate.

You can write

return (ApplicationViewModel)Invoke(new Func<object>(() => applicationsCombobox.SelectedItem));


回答3:

Try:

public ApplicationViewModel SelectedApplication
    {
        get
        {
            if (InvokeRequired)
            {
                BeginInvoke(() => { return SelectedApplication(); });
            }

            return _applicationsCombobox.SelectedItem as ApplicationViewModel;
        }
    }