I have following Application Structure:
public partial class MainWindow : Window
{
// Methos to update the TextBlock
public void updateTextBlock(string txt)
{
this.myTextBlock.Text += txt;
}
private void startThreadBtn_Click(object sender, RoutedEventArgs e)
{
// Start Thread1 here
Thread1 thread1 = new Thread1();
new Thread(new ThreadStart(thread1.doSomthing)).Start();
}
}
class Thread1
{
public void doSomthing()
{
// ------------------------------
// Update myTextBlock from here
// ------------------------------
// Thread2 starts here
Thread2 thread2 = new Thread2();
new Thread(new ThreadStart(thread2.doSomthing)).Start();
}
}
class Thread2
{
public void doSomthing()
{
// ------------------------------
// Update myTextBlock from here
// ------------------------------
}
}
From both of these threads Thread1
and Thread2
classes I want to update the TextBlock
which is in the MainWindow
.
I have seen following solutions, and don't find this condition covered in this questions, also i am beginner and find it way difficult to understand.
- How to update UI from another thread running in another class
- How to update the GUI from another thread in C#?
- Update UI from background Thread
I can use solutions provided in the above questions for Thread1
but what about updating UI from Thread2
.
I am using .NET framework 4.5.
What is the best way of doing it.