DispatcherObject.CheckAccess 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
确定调用线程是否有权访问此 DispatcherObject权限。
public:
bool CheckAccess();
public bool CheckAccess();
member this.CheckAccess : unit -> bool
Public Function CheckAccess () As Boolean
返回
true 如果调用线程有权访问此对象,则为 ;否则,为 false.
示例
以下示例用于 CheckAccess 确定线程是否有权访问创建线程的线程 Button 。 调用 CheckAccess 该方法 Button 以验证对线程的访问。 如果调用线程具有访问权限, Button 则只需访问成员 Button即可更新该线程;否则,接受 Button 作为参数的委托将 Dispatcher 发布到其中 Button。
// Uses the DispatcherObject.CheckAccess method to determine if
// the calling thread has access to the thread the UI object is on
private void TryToUpdateButtonCheckAccess(object uiObject)
{
Button theButton = uiObject as Button;
if (theButton != null)
{
// Checking if this thread has access to the object
if (theButton.CheckAccess())
{
// This thread has access so it can update the UI thread
UpdateButtonUI(theButton);
}
else
{
// This thread does not have access to the UI thread
// Pushing update method on the Dispatcher of the UI thread
theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new UpdateUIDelegate(UpdateButtonUI), theButton);
}
}
}
' Uses the DispatcherObject.CheckAccess method to determine if
' the calling thread has access to the thread the UI object is on
Private Sub TryToUpdateButtonCheckAccess(ByVal uiObject As Object)
Dim theButton As Button = TryCast(uiObject, Button)
If theButton IsNot Nothing Then
' Checking if this thread has access to the object
If theButton.CheckAccess() Then
' This thread has access so it can update the UI thread
UpdateButtonUI(theButton)
Else
' This thread does not have access to the UI thread
' Pushing update method on the Dispatcher of the UI thread
theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New UpdateUIDelegate(AddressOf UpdateButtonUI), theButton)
End If
End If
End Sub
注解
只有创建时创建的线程才能访问该Dispatcher线程DispatcherObject。
任何线程都可以检查它是否有权访问此 DispatcherObject线程。
区别在于CheckAccessVerifyAccessCheckAccess,返回一个布尔值,该值指定调用线程是否有权访问此对象,并在DispatcherObject调用线程无权VerifyAccess访问此DispatcherObject对象时引发异常。
调用此方法与调用 CheckAccess 关联的 Dispatcher 对象相同。