我正在将使用InvokeRequired
的VB表单代码转换为C# WPF。
// VB
If Me.InvokeRequired Then
MessageBox.Show("Apple")
Else
MessageBox.Show("Orange")
End If
VB代码返回"Apple“
// C#
if (this.Dispatcher.CheckAccess())
{
MessageBox.Show("Apple");
}
else
{
MessageBox.Show("Orange");
}
但是我的C#代码返回"Orange“
C#的CheckAccess
不是和VB的InvokeRequired
相同吗?为什么布尔值是倒的?
发布于 2012-03-16 10:42:48
Dispatcher.CheckAccess()
检查是否与线程相关联,在本例中为false。
InvokeRequired
检查是否需要与线程相关联,在本例中为真。
当不与特定线程关联时,需要调用(InvokeRequired == true
)。换言之,所需调用的逻辑定义是:
InvokeRequired = !Dispatcher.CheckAccess()
如果无法访问特定线程,则需要调用。
发布于 2012-03-16 10:30:04
我猜这是说InvokeRequired
意味着您需要在GUI线程上调用,而CheckAccess
则检查当前线程是否有访问该GUI的权限,如果有访问权限,将返回true
。
用这种方式更改命名方式似乎有点令人困惑。
NB.C#具有Windows.Forms
的InvokeRequired
属性
https://stackoverflow.com/questions/9735583
复制相似问题