I have a WPF window that creates and starts a timer in its constructor. The timer elapsed event triggers a method (SyncPTUpdate) which uses BeginInvoke to place a call to another method (PTProgressUpdateInThread) onto the Window's thread. This then calls a WCF call asynchronously (using the TAP pattern, auto-generated by VS 2013).
When I make the WCF call artificially long in duration (using thread.sleep in the server component), the UI of my WPF application freezes. Not initially, but after a few seconds have gone by.
Where am I going wrong?
public delegate void PTProgressDelegate();
// this method is called from a periodically firing timer (System.Timers)
private async void SyncPTUpdate(object sender, ElapsedEventArgs e)
{
await this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new PTProgressDelegate(PTProgressUpdateInThread));
}
private async void PTProgressUpdateInThread()
{
PTMapFieldClient = new FieldClient();
ServiceField.BlokPTProgress PTProgressFromServer = await PTMapFieldClient.GetPTProgressAsync(variousparametershere);
PTMapFieldClient.Close();
// now use the results of the WCF call to update the Window UI
//...
}
Dispatcher.BeginInvoke()
is often presented as a way to do things async, with the benefit of not having to create/involve another thread.
But that means it is only beneficial for small, lightweight jobs. Best thing here is to push the call(s) to PTProgressUpdateInThread() to the ThreadPool. Or yuse a threaded Timer.
You're not using the results after await
anyway.