Search code examples
multithreadingsilverlightbackgroundworkerwcf-ria-services

Run RIA service on non UI thread


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


Solution

  • 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.

    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