Check Whether You Are Running on the UI Thread
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF" Height="120" Width="364">
<StackPanel>
<Button Click="ButtonTrue_Click">UI Thread</Button>
<Button Click="ButtonFalse_Click">Non-UI Thread</Button>
<TextBlock x:Name="txtResult"/>
</StackPanel>
</Window>
//File:Window.xaml.vb
Imports System.Windows
Imports System.Windows.Threading
Namespace WpfApplication1
Public Partial Class Window1
Inherits Window
Private Delegate Sub CheckAccessDelegate()
Private Delegate Sub SetResultTextDelegate(result As String)
Public Sub New()
InitializeComponent()
End Sub
Private Sub ButtonTrue_Click(sender As Object, e As RoutedEventArgs)
CheckAccess()
End Sub
Private Sub ButtonFalse_Click(sender As Object, e As RoutedEventArgs)
Dim del As New CheckAccessDelegate(AddressOf CheckAccess)
del.BeginInvoke(Nothing, Nothing)
End Sub
Private Overloads Sub CheckAccess()
If txtResult.Dispatcher.CheckAccess() Then
txtResult.Text = "True"
Else
txtResult.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New SetResultTextDelegate(AddressOf SetResultText), "False")
End If
End Sub
Private Sub SetResultText(result As String)
txtResult.Text = result
End Sub
End Class
End Namespace
Related examples in the same category