Run RIA service on non UI thread

白昼怎懂夜的黑 提交于 2019-12-07 09:29:46

问题


I am trying to make RIA service calls from non UI thread.
I made calls with opening new thread and with background workers, but for both cases callback is running on UI thread.
Is it possible to execute callback on callers thread, not UI?
Thanks


回答1:


tl;dr

  • Use WCF
  • Marshal your results to the UI thread yourself

WCF RIA is built to do work on the UI thread

WCF RIA was designed to work mostly on the UI thread. That obviously has many pros and cons; mostly cons in your case. I'm having trouble finding definitive documentation of this design, however, most questions on the topic are answered by affirming the UI threadedness.

  • Here is a post from a member of the WCF RIA QA team indicating UI-thread-only processing.
  • Andy French describes the callbacks marshalling to the UI thread:

The Domain Context Load and SubmitChanges execute asynchronously. They take a thread from the thread pool, make the necessary calls to the server, and when those calls complete the work is automatically marshalled back to the UI thread to modify the entity collections and subsequently update the UI (probably via INotificationChanged).

If you use WCF to get your own data, you can do it on any thread you like. When the calls complete, you will have to write (or borrow) the code to update the UI on the UI thread to avoid cross thread exceptions.

IMO, the main advantages of WCF RIA giving simple problems simple solutions:

  • Great tooling for re-using code between the server and client
  • Service and client code are always compatible
  • Transferring data to/from the client/server is relatively simple
  • WCF RIA is strongly opinionated resulting in easy-to-learn coding patterns

The cons make hard problems hard or impossible:

  • WCF RIA is strongly opinionated and not following that opinion is painful or impossible
  • All operations return on the UI thread, often causing performance problems
  • There is some voodoo to achieve the highest amount of client+server code re-use


来源:https://stackoverflow.com/questions/11970092/run-ria-service-on-non-ui-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!