Search code examples
c#invokerequired

C# InvokeRequired with property getter


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

Solution

  • 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.