简体   繁体   中英

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

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.

BeginInvoke takes a delegate.

You can write

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

Try:

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

            return _applicationsCombobox.SelectedItem as ApplicationViewModel;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM