带属性 getter 的 C# InvokeRequired
我想让我的 getter 线程安全。当我这样做时,我收到一个错误:
public ApplicationViewModel SelectedApplication
{
get
{
if (InvokeRequired)
{
BeginInvoke((Action<ApplicationViewModel>)SelectedApplication);
}
return _applicationsCombobox.SelectedItem as ApplicationViewModel;
}
}
我有错误:
Cannot cast expression of type 'Foo.Model.ApplicationViewModel' to type 'Action<ApplicationViewModel>'
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>'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
BeginInvoke
接受委托。你可以写
BeginInvoke
takes a delegate.You can write
尝试:
Try:
很多事情都是错误的:
Invoke
Func<> ;
为必填项,else
为必填项。这会产生:
在低级属性中隐藏线程上下文切换通常是一个错误。调用会产生大量开销,如果没有很好地提示为什么这么慢,生成的代码可能会非常慢。
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.