Dispatcher.CheckAccess 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
判斷呼叫執行緒是否即為與此 Dispatcher執行緒相關聯的執行緒。
public:
bool CheckAccess();
public bool CheckAccess();
member this.CheckAccess : unit -> bool
Public Function CheckAccess () As Boolean
傳回
true 若呼叫執行緒是與此 Dispatcher關聯的執行緒;否則, false。
範例
以下範例用 CheckAccess 來判斷執行緒是否能存取 Button。 CheckAccess與 相關的 Dispatcher 方法Button被呼叫以驗證對執行緒的存取。 若呼叫執行緒能存取 Dispatcher, Button 則透過存取 的成員 Button進行更新;否則,接受 參數 Button 的代理將被置於 Dispatcher。 他們 Dispatcher 將委派更新 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
備註
只有 a Dispatcher 所建立的 該DispatcherObject物件才能存取該物件。 使用 Invoke 或 BeginInvoke 從不同執行緒存取該物件。
CheckAccess 可以從任何執行緒呼叫。
與 CheckAccessVerifyAccess 之間的差異CheckAccess會回傳一個布林值,表示呼叫執行緒是否有權限存取 並DispatcherVerifyAccess拋出例外。