Solution :
I had also faced the similar issue in the recent past. I did lot of research on it and found the solution on it.This is the very common problem with the people getting started. Whenever you need to update your UI elements from the thread other than a main thread, you need to use as below :
this.Dispatcher.Invoke(() =>
{
...// your code goes here.
});
You can also try to use control.Dispatcher.CheckAccess()
to check whether your current thread owns a control. If it does own it then your code looks as the normal. Otherwise, use the above mentioned pattern.
OR
Another good use for the Dispatcher.Invoke
is for the immediately updating your UI in the function that performs the other tasks:
// Force WPF to render the UI changes immediately with below magic line of code...
Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle);
I use above code to update button text to "Processing..." and then disable it while making the WebClient
requests.