开发者

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 开发者_Python百科than 1000ms How to avoid hanhing UI. Plz provide some hints to implement


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


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)


You can use Asynchronous Web Service call to communicate with the web service.

examples:

http://patconroy.wordpress.com/2009/01/22/asynchronous-web-service-invocation/

http://www.codeguru.com/csharp/csharp/cs_webservices/security/article.php/c9179

http://ondotnet.com/pub/a/dotnet/2005/08/01/async_webservices.html


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜