I am building a WPF application that calls web services and displays the data returned from the service after being broken down and analyzed by my application. The problem that I am facing is with multithreading. One of the API calls is made using a DispatcherTimer every 60 seconds. The issue is that when this event fires, it blocks the UI thread. I have attempted (in all ways that I can think) to update the UI from the background thread using BackgroundWorker and Dispatcher objects (also delegates) and I cannot figure this out. I need an example showing a label on the UI thread being updated by the background thread. Any help with this would be fantastic as I am about to freak out :).
I have looked at the other articles and it is just not making a terrible amount of sense to me. Please, bear with me as I am pretty new to this. Here is an example of what I would like to do. I have a label on the window named lblCase. I call pullData() every 60 seconds and I want to update lblCase with the returned data without blocking the UI.
private void pullData()
{
//API call goes here...
lblCase.Content = iCase;
}
public MainWindow()
{
InitializeComponent();
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(0,0,60);
timer.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
pullData();
}
Have a look at this question…
cheers,
EDIT:
Joe – not sure if you’re getting any closer to groking this, so I thought I’d try to put together a simple usage of BackgroundWorker to demonstrate how simple and powerful this class is!
first – in your constructor…
so we have set up something to delegate some work (in the method ‘worker_DoWork’) on a background thread… whatever happends in that method will not impact the UI thread, and it should look something like:
Now when this thread completes, it will fire the RunWorkerCompleted event, which we have handled as such:
Hope this helps!
IanR