I’ve got a WPF application written in C#. It makes a call to a WCF service I wrote. At times, the WCF service could take up to 20 seconds to return (depending on if it needs to refresh data). I know I can make the WCF service support asynchronous calls, but another solution I thought would work would be to wrap the call to the WCF service into a new thread. I did so with the following code:
new System.Threading.Thread(
new System.Threading.ThreadStart(
delegate()
{
Action del = delegate()
{
MyService.MyServiceClient ms = new MyService.MyServiceClient();
lblTotalCost.Text = ms.GetTotalCost().ToString("C");
};
this.Dispatcher.BeginInvoke(del);
})).Start();
I put this into the constructor function of one of my UserControls, after InitializeComponent().
Without it, the application won’t appear until the service call completes. My hope was that adding this would make it so that the application would appear immediately and that the label would populate once the service call completed. To my surprise though, this did not happen. The application still does not appear until the service call completes.
How does this need to be modified so that it does what I intended it to do?
Thanks!
Using the
Dispatcheryou put all the work back on the UI-thread. Only the assignment tolblTotalCost.Textshould be done there i suppose.