DispatcherObject.CheckAccess Metodo
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Determina se il thread chiamante ha accesso a DispatcherObject.
public:
bool CheckAccess();
public bool CheckAccess ();
member this.CheckAccess : unit -> bool
Public Function CheckAccess () As Boolean
true
se il thread chiamate ha accesso a questo oggetto; in caso contrario, false
.
Nell'esempio seguente viene CheckAccess usato per determinare se un thread ha accesso al thread in cui è stato creato un oggetto Button . Il CheckAccess metodo in Button viene chiamato per verificare l'accesso al thread. Se il thread chiamante ha accesso, l'oggetto Button viene aggiornato semplicemente accedendo ai membri di Button; in caso contrario, un delegato che accetta come Button argomento viene inserito nell'oggetto Dispatcher Buttondi .
// 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
Solo il thread Dispatcher in cui è stato creato può accedere a DispatcherObject.
Qualsiasi thread può verificare se ha accesso a questo DispatcherObjectoggetto .
La differenza tra CheckAccess e VerifyAccess è che CheckAccess restituisce un valore booleano che specifica se il thread chiamante ha accesso a questo DispatcherObject oggetto e VerifyAccess genera un'eccezione se il thread chiamante non ha accesso all'oggetto DispatcherObject.
La chiamata a questo metodo è identica alla chiamata CheckAccess all'oggetto associato Dispatcher .
Prodotto | Versioni |
---|---|
.NET Framework | 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8 |
Windows Desktop | 3.0, 3.1, 5, 6, 7 |