I'm trying to get a BackgroundWorker to update the UI during program logic execution. But I get the error:
EDIT! So my actual goal, which I guess isn't clear from the sample I provided, is to be able to perform calculations while updating the UI. See the updated code sample below.
InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
My C# and xaml follow:
public partial class MainWindow : Window
{
private readonly BackgroundWorker worker = new BackgroundWorker();
public MainWindow()
{
InitializeComponent();
worker.DoWork += worker_DoWork;
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
test.Background = Brushes.Orange;
for (double x = 0; x < 10000000000; )
{
x++;
}
test.Background = Brushes.Red;
}
private void test_Click(object sender, RoutedEventArgs e)
{
worker.RunWorkerAsync();
}
}
And then, my XAML:
<Button Name="test" Click="test_Click">This is a button!</Button>