I have a button, when I click it, I want it to update the UI with the value
this is my button
private async void Button_Click(object sender, RoutedEventArgs e)
{
await DoItAsync();
}
and this is DoIt!
public async Task DoSomethingAsync()
{
await Task.Run(()=>{
for(int i = 0; i < 2000; i++)
{
//labelName.Content = i.ToString();
Console.WriteLine(i);
}
});
}
so if I run the above code, click my button, DoItAysnc is called and all the numbers are displayed in the console.
If I un-comment
labelName.Content = i.ToString();
I get an exception, the following in fact : The calling thread cannot access this object because a different thread owns it.
I know Async and await don't solve threading for you, but I did think this would work. I would like to know how to get this to work so that the UI is updated for each i value without the UI becoming unresponsive.
Any good resources on this would be helpful also.
As always, kinds regards