Dispatcher.VerifyAccess 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 Dispatcher.
public:
void VerifyAccess();
public void VerifyAccess();
member this.VerifyAccess : unit -> unit
Public Sub VerifyAccess ()
Undantag
Den anropande tråden har inte åtkomst till den här Dispatcher.
Exempel
I följande exempel används VerifyAccess för att avgöra om en tråd har åtkomst till den tråd som en Button skapades på. Metoden tar ett objekt som ett argument, som är gjutet till en Button. Metoden VerifyAccess i Dispatcher anropas Button för att verifiera åtkomsten till tråden.
Om den anropande tråden DispatcherButton har åtkomst till uppdateras den genom att bara komma åt medlemmarna i Button.
Om den anropande tråden inte har åtkomst genereras en InvalidOperationException . Det här exemplet fångar undantaget och push-överför ett ombud, som accepterar ett Button som argument, till Dispatcher i Button. Detta Dispatcher gör arbetet med att uppdatera Button.
// Uses the Dispatcher.VerifyAccess method to determine if
// the calling thread has access to the thread the UI object is on.
private void TryToUpdateButtonVerifyAccess(object uiObject)
{
Button theButton = uiObject as Button;
if (theButton != null)
{
try
{
// Check if this thread has access to this object.
theButton.Dispatcher.VerifyAccess();
// The thread has access to the object, so update the UI.
UpdateButtonUI(theButton);
}
// Cannot access objects on the thread.
catch (InvalidOperationException e)
{
// Exception Error Message.
MessageBox.Show("Exception ToString: \n\n" + e.ToString(),
"Exception Caught! Thrown During AccessVerify().");
MessageBox.Show("Pushing job onto UI Thread Dispatcher");
// Placing job onto the Dispatcher of the UI Thread.
theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new UpdateUIDelegate(UpdateButtonUI), theButton);
}
}
}
' Uses the Dispatcher.VerifyAccess method to determine if
' the calling thread has access to the thread the UI object is on.
Private Sub TryToUpdateButtonVerifyAccess(ByVal uiObject As Object)
Dim theButton As Button = TryCast(uiObject, Button)
If theButton IsNot Nothing Then
Try
' Check if this thread has access to this object.
theButton.Dispatcher.VerifyAccess()
' The thread has access to the object, so update the UI.
UpdateButtonUI(theButton)
' Cannot access objects on the thread.
Catch e As InvalidOperationException
' Exception Error Message.
MessageBox.Show("Exception ToString: " & vbLf & vbLf & e.ToString(), "Execption Caught! Thrown During AccessVerify().")
MessageBox.Show("Pushing job onto UI Thread Dispatcher")
' Placing job onto the Dispatcher of the UI Thread.
theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New UpdateUIDelegate(AddressOf UpdateButtonUI), theButton)
End Try
End If
End Sub
Kommentarer
Endast den tråd som Dispatcher skapas på kan komma åt Dispatcher.
Den här metoden är offentlig. Därför kan alla trådar kontrollera om den har åtkomst till Dispatcher.
Skillnaden mellan CheckAccess och returnerar VerifyAccess ett booleskt värde om den anropande tråden inte har åtkomst till CheckAccess och Dispatcher genererar ett VerifyAccess undantag.