Dispatcher.CheckAccess Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Détermine si le thread appelant est le thread associé à ce Dispatcher.
public:
bool CheckAccess();
public bool CheckAccess();
member this.CheckAccess : unit -> bool
Public Function CheckAccess () As Boolean
Retours
true si le thread appelant est le thread associé à ceci Dispatcher; sinon, false.
Exemples
L’exemple suivant utilise CheckAccess pour déterminer si un thread a accès à un Button. La CheckAccess méthode associée DispatcherButton est appelée pour vérifier l’accès au thread. Si le thread appelant a accès au Dispatcherthread d’appel, il Button est mis à jour en accédant aux membres du Button; sinon, un délégué, qui accepte un Button argument, est placé sur le Dispatcher. Le Dispatcher délégué du travail de mise à jour du Button.
// 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
Remarques
Seul l’objet Dispatcher sur lequel un DispatcherObject est créé peut accéder à l’objet. Utilisez ou Invoke accédez BeginInvoke à l’objet à partir d’un autre thread.
CheckAccess peut être appelé à partir de n’importe quel thread.
La différence entre CheckAccess et VerifyAccess renvoie CheckAccess une valeur booléenne indiquant si le thread appelant a accès au Dispatcher thread appelant et VerifyAccess lève une exception.