Dispatcher.VerifyAccess Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Bepaalt of de aanroepende thread toegang heeft tot dit Dispatcher.
public:
void VerifyAccess();
public void VerifyAccess();
member this.VerifyAccess : unit -> unit
Public Sub VerifyAccess ()
Uitzonderingen
De aanroepende thread heeft hier geen toegang toe Dispatcher.
Voorbeelden
In het volgende voorbeeld wordt gebruikt VerifyAccess om te bepalen of een thread toegang heeft tot de thread waarop een Button thread is gemaakt. De methode neemt een object als argument, dat wordt omgezet in een Button. De VerifyAccess methode op de van de DispatcherButton thread wordt aangeroepen om de toegang tot de thread te verifiëren.
Als de aanroepende thread toegang heeft tot de Dispatcherthread, wordt deze Button bijgewerkt door alleen toegang te krijgen tot de leden van de Buttonthread.
Als de aanroepende thread geen toegang heeft, wordt er een InvalidOperationException gegenereerd. In dit voorbeeld wordt de uitzondering onderschept en wordt een gemachtigde, die een Button als argument accepteert, naar de van de DispatcherButtongemachtigde gepusht. Dit Dispatcher doet het werk van het bijwerken van de 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
Opmerkingen
Alleen de thread waarop de Dispatcher thread is gemaakt, heeft toegang tot de Dispatcher.
Deze methode is openbaar; daarom kan elke thread controleren of deze toegang heeft tot de Dispatcher.
Het verschil tussen CheckAccess en VerifyAccess wordt CheckAccess een Booleaanse waarde geretourneerd als de aanroepende thread geen toegang heeft tot de Dispatcher thread en VerifyAccess een uitzondering genereert.