DispatcherObject.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 a accès à ce DispatcherObjectfichier .
public:
bool CheckAccess();
public bool CheckAccess();
member this.CheckAccess : unit -> bool
Public Function CheckAccess () As Boolean
Retours
true si le thread appelant a accès à cet objet ; sinon, false.
Exemples
L’exemple suivant montre CheckAccess comment déterminer si un thread a accès au thread sur lequel un thread Button a été créé. La CheckAccess méthode sur le Button thread est appelée pour vérifier l’accès au thread. Si le thread appelant a accès, il Button est mis à jour en accédant simplement aux membres du Button; sinon, un délégué, qui accepte un Button argument, est publié sur le DispatcherButton.
// 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
Remarques
Seul le thread sur lequel l’utilisateur Dispatcher a été créé peut accéder au DispatcherObjectfichier .
N’importe quel thread peut vérifier s’il a accès à ce DispatcherObjectfichier .
La différence entre CheckAccess et VerifyAccess est que CheckAccess retourne un booléen qui spécifie si le thread appelant a accès à ceci DispatcherObject et VerifyAccess lève une exception si le thread appelant n’a pas accès à ce DispatcherObject.
L’appel de cette méthode est identique à l’appel CheckAccess sur l’objet associé Dispatcher .