DispatcherObject.CheckAccess Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Avgör om den anropande tråden har åtkomst till den här DispatcherObject.
public:
bool CheckAccess();
public bool CheckAccess();
member this.CheckAccess : unit -> bool
Public Function CheckAccess () As Boolean
Returer
trueom den anropande tråden har åtkomst till det här objektet; annars . false
Exempel
I följande exempel används CheckAccess för att avgöra om en tråd har åtkomst till den tråd som en Button skapades på. Metoden CheckAccess på anropas Button för att verifiera åtkomsten till tråden. Om den anropande tråden Button har åtkomst uppdateras den genom att bara komma åt medlemmarna i Button. Annars publiceras ett ombud, som accepterar ett Button som ett argument, på Dispatcher .Button
// 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
Kommentarer
Endast tråden som Dispatcher skapades på kan komma åt DispatcherObject.
Alla trådar kan kontrollera om den har åtkomst till den här DispatcherObject.
Skillnaden mellan CheckAccess och är att VerifyAccess returnerar ett booleskt värde som anger om den anropande tråden har åtkomst till detta CheckAccess och DispatcherObject utlöser ett undantag om den anropande tråden inte har åtkomst till den här VerifyAccessDispatcherObject .
Att anropa den här metoden är identiskt med att anropa CheckAccess det associerade Dispatcher objektet.