user517206
user517206

Reputation: 161

Avoid hanging UI in WPF app

Iam new to MVVM and WPF. MY WPF app will call web service every 2 min and display the response in UI. Provide a chance to manually call same web service by clicking a button in UI. My web service timeout is 5000ms. My Q is if web service take more than 1000ms How to avoid hanhing UI. Plz provide some hints to implement

Upvotes: 2

Views: 1743

Answers (5)

Naveen Chakravarthy
Naveen Chakravarthy

Reputation: 839

Use Background worker and if you want to return the data to the UI use Dispatcher. If using .net 4.0 use Tasks.

Upvotes: 0

GWLlosa
GWLlosa

Reputation: 24433

Basically, you should not be doing any work on the UI thread which can take any time to execute. You should have in your View Model some form of a Command object which is databound to your View's button. That command should, on another thread, asynchronously invoke your web service call. That way, the UI will never be hung, waiting for the command to return, because the command will return very quickly. When the asynchronous call returns, you can handle updating any UI stuff you need to do then.

Upvotes: 1

Justin Niessner
Justin Niessner

Reputation: 245479

How about using Threading? MSDN has a good write-up from back in 2007 on the basics:

WPF Threads: Build More Responsive Apps with The Dispatcher

Make sure you don't only pay attention to the Dispatcher but also the second on BackgroundWorker.

If you're not familiar with Threading at all (which seems like it might be the case), then you should probably start out with the plain Threading basics from MSDN:

Threading (C# and Visual Basic)

Upvotes: 3

Jamie Keeling
Jamie Keeling

Reputation: 9966

This would be a good place to implement multiple threads, performing all of the web app communication in one thread allows the main UI thread to be responsive to the users input.

Following are a few links to get you going:

  1. Background Worker
  2. Multithreading in WPF
  3. Getting started with Multithreading & WPF

Upvotes: 5

Related Questions