DispatcherObject.CheckAccess 方法

定义

确定调用线程是否可以访问此 DispatcherObject

C#
public bool CheckAccess();

返回

如果调用线程可以访问此对象,则为 true;否则,为 false

示例

以下示例使用 CheckAccess 来确定线程是否有权访问创建 的线程 ButtonCheckAccess调用 上的 Button 方法以验证对线程的访问。 如果调用线程具有访问权限, Button 则仅通过访问 的成员 Button来更新 ;否则,接受 Button 作为参数的 委托将发布到 Dispatcher 的 上 Button

C#
// 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);
        }
    }
}

注解

只有创建 的线程 Dispatcher 才能访问 DispatcherObject

任何线程都可以检查它是否有权访问此 DispatcherObject

VerifyAccess 之间的差异CheckAccess是 返回CheckAccess一个布尔值,指定调用线程是否有权访问此 DispatcherObject ,并在VerifyAccess调用线程无权访问此 DispatcherObject时引发异常。

调用此方法与对关联的 Dispatcher 对象调用CheckAccess相同。

适用于

产品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10