I was trying to learn something about the thread in WPF
this is the code snippet:
public MainWindow()
{
InitializeComponent();
start = delegate()
{
statusText.Text = "From Other Thread";
};
t = new Thread(start);
t.Start();
}
as the book said it will have exceptions, and so it is. I learned that is because something about the dispatch thread. Then I put a code line into the button-click processing function as the code below :`
public partial class MainWindow : Window
{
ThreadStart start;
Thread t;
public MainWindow()
{
InitializeComponent();
start = delegate()
{
statusText.Text = "From Other Thread";
};
t = new Thread(start);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
t.Start();
}
I was hoping that it will have exceptions as before, but nothing happened, and the text was not changed. so weired for me. why this happened? Hope someone can offer some help.Thanks.
In order to update a ui component from another thread you have to use the Dispatcher: