Dispatcher.VerifyAccess Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Určuje, zda volající vlákno má přístup k tomuto Dispatcher.
public:
void VerifyAccess();
public void VerifyAccess();
member this.VerifyAccess : unit -> unit
Public Sub VerifyAccess ()
Výjimky
Volající vlákno nemá přístup k tomuto Dispatcher.
Příklady
Následující příklad používá VerifyAccess k určení, zda vlákno má přístup k vláknu, na které Button byl vytvořen. Metoda přebírá objekt jako argument, který je přetypován na Button. Metoda VerifyAccess na Dispatcher straně Button je volána k ověření přístupu k vláknu.
Pokud volající vlákno má přístup k Dispatcher, Button je aktualizován pouze přístupem k členům Button.
Pokud volající vlákno nemá přístup, InvalidOperationException je vyvolán. Tento příklad zachytí výjimku a nasdílí delegáta, který přijímá Button jako argument do Dispatcher objektu Button. Tím Dispatcher se provede aktualizace souboru 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
Poznámky
Pouze vlákno, na které Dispatcher je vytvořeno, může přistupovat k Dispatcher.
Tato metoda je veřejná; proto může jakékoli vlákno zkontrolovat, zda má přístup k Dispatcher.
Rozdíl mezi CheckAccess a VerifyAccess je CheckAccess vrátí logickou hodnotu, pokud volající vlákno nemá přístup k Dispatcher a VerifyAccess vyvolá výjimku.