Home:ALL Converter>Updating UI from a background Thread and thread within a Thread

Updating UI from a background Thread and thread within a Thread

Ask Time:2014-10-10T14:51:22         Author:User7723337

Json Formatter

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.

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.

Author:User7723337,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/26293506/updating-ui-from-a-background-thread-and-thread-within-a-thread
yy