So, today I was working on a WPF project where I had a background working thread, and I had to update the progress value of the progressbar according to the progress of the work being done in the thread.
Now, when we created a background worker thread in Windows Forms, we needed to invoke the UI thread to update it:
//Updating a label
private delegate void UpdateUI(string Text)
protected void UpdateLabel(string text)
{
if(this.InvokeRequired)
{
UpdateUI UpdateLabel = new UpdateUI(this.UpdateLabel);
object[] Args=new object[1]("UpdatedText");
this.Invoke(UpdateLabel , Args);
}
else
this.Label.Text=text;
}
But I couldn't find how to implement this in WPF as this wont work in WPF.
After looking at a few pages here and there, i finally found a workaround:
this.Dispatcher.Invoke((Action)(() =>{
Label1.Text = "Updated Text";
}));
Just call Dispatcher.Invoke(delegate Method,params object[] args), and pass a delegate (or a lambda expression casted to type 'action' ), and update the UI element inside.
Happy Coding.