How should I edit WPF data that exists outside a C# task? I have defined a UI using the standard WPF template. The grid contains only:
<TextBlock x:Name="myTextBox" Text="Original Text" />
In my code I have:
using System.Threading.Tasks;
using System.Windows;
namespace UpdateGUI
{
public partial class MainWindow : Window
{
private int myInt;
public MainWindow()
{
InitializeComponent();
myInt = 0;
Task.Factory.StartNew(() =>
{
myInt++;
myTextBox.Text = "New Text";
});
}
}
}
This throws the following exception:
The calling thread cannot access this object because a different thread owns it.
Which thread owns myTextBox? Why don't I see this error when I edit myInt?