英語で読む

次の方法で共有


DispatcherObject.CheckAccess メソッド

定義

呼び出し元のスレッドがこの DispatcherObject にアクセスできるかどうかを確認します。

C#
public bool CheckAccess();

戻り値

呼び出し元のスレッドがこのオブジェクトにアクセスできる場合は true。それ以外の場合は false

次の例では、 を使用 CheckAccess して、 が作成されたスレッドにスレッドが Button アクセスできるかどうかを判断します。 の メソッドButtonCheckAccess、スレッドへのアクセスを確認するために呼び出されます。 呼び出し元のスレッドにアクセス権がある場合は、 ButtonButtonメンバーにアクセスするだけで が更新されます。それ以外の場合は、 を引数として受け入れるButtonデリゲートが の ButtonDispatcherポストされます。

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);
        }
    }
}

注釈

にアクセスDispatcherObjectできるのは、 Dispatcher が作成されたスレッドだけです。

どのスレッドでも、この 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