Dispatcher.CheckAccess 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 je vlákno přidružené k tomuto Dispatcher.
public:
bool CheckAccess();
public bool CheckAccess();
member this.CheckAccess : unit -> bool
Public Function CheckAccess () As Boolean
Návraty
true je-li volající vlákno vlákno přidružené k tomuto Dispatcher; v opačném případě , false.
Příklady
Následující příklad používá CheckAccess k určení, zda vlákno má přístup k .Button Metoda CheckAccess na přidružené k Dispatcher volat Button je ověřit přístup k vláknu. Pokud má volající vlákno přístup k Dispatcher, Button je aktualizován přístupem k členům Button; v opačném případě delegát, který přijímá Button jako argument, je umístěn na Dispatcher. Bude Dispatcher delegovat práci aktualizace Button.
// Uses the Dispatcher.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.Dispatcher.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.
// Place the update method on the Dispatcher of the UI thread.
theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new UpdateUIDelegate(UpdateButtonUI), theButton);
}
}
}
' Uses the Dispatcher.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.Dispatcher.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.
' Place the 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
Poznámky
Dispatcher K objektu může přistupovat pouze objekt DispatcherObject vytvořený objektem. Použijte Invoke nebo BeginInvoke pro přístup k objektu z jiného vlákna.
CheckAccess lze volat z libovolného vlákna.
Rozdíl mezi CheckAccess a VerifyAccess je CheckAccess vrátí logickou hodnotu označující, zda volající vlákno má přístup k Dispatcher a VerifyAccess vyvolá výjimku.