Dispatcher.CheckAccess Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Bestimmt, ob der aufrufende Thread der diesem DispatcherThread zugeordnet ist.
public:
bool CheckAccess();
public bool CheckAccess();
member this.CheckAccess : unit -> bool
Public Function CheckAccess () As Boolean
Gibt zurück
truewenn der aufrufende Thread der diesem DispatcherThread zugeordnet ist; andernfalls . false
Beispiele
Im folgenden Beispiel wird CheckAccess verwendet, um zu bestimmen, ob ein Thread Zugriff auf ein Button. Die CheckAccess Methode für die Dispatcher zugeordnete Methode Button wird aufgerufen, um den Zugriff auf den Thread zu überprüfen. Wenn der aufrufende Thread Zugriff auf den DispatcherThread hat, wird die Button Aktualisierung durch Den Zugriff auf die Member der Button; andernfalls wird eine Stellvertretung, die ein Button Argument akzeptiert, auf das Dispatchergesetzt. Die Dispatcher Arbeit der Aktualisierung wird Buttondelegieren.
// Uses the Dispatcher.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.Dispatcher.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.
// Place the update method on the Dispatcher of the UI thread.
theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new UpdateUIDelegate(UpdateButtonUI), theButton);
}
}
}
' Uses the Dispatcher.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.Dispatcher.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.
' Place the 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
Hinweise
Nur das Dispatcher , für das ein DispatcherObject Objekt erstellt wird, kann auf das Objekt zugreifen. Verwenden Invoke oder BeginInvoke zugreifen Sie auf das Objekt aus einem anderen Thread.
CheckAccess kann von einem beliebigen Thread aufgerufen werden.
Der Unterschied zwischen CheckAccess und VerifyAccess gibt CheckAccess einen Booleschen Wert zurück, der angibt, ob der aufrufende Thread Zugriff auf die Dispatcher und VerifyAccess löst eine Ausnahme aus.