C# – How to use Dispatcher with async call in WP7

asynchronouscdispatchersilverlightwindows-phone-7

I haven't been able to find an example like this, though I'm sure there must be a few out there.

When the user clicks a button to log in, an event handler on the button click calls a function that logs the user in. Based on the user, they can be taken to one of many start screens. The information for which screen is returned from a service call. From what I can tell, Dispatcher.BeginInvoke is only used to update the UI thread, so the logic that determines which page to navigate to should be in the method passed to Dispatcher.BeginInvoke, right?

I need to make a service call and take action based on the result. Do I have to make the async service call first and call Dispatcher from the callback? Do I put the function that does the validation, calls the service, and handles the callback as the delegate that is passed to the Dispatcher?

Sorry if this is a basic question. The examples I've found only use the Dispatcher to update a textbox or some other trivial item. I haven't found anything where the UI thread needs to take action based on the result of an async call.

Best Answer

It's not clear what validation you're talking about but:

  • Call the service asynchronously, with a callback to execute when the service call finishes
  • In the callback, do whatever non-UI related work is involved, and then call Dispatcher.BeginInvoke to perform any UI-related operations.

If you need to do validation before the service call, that could be part of your button's event handler... at least as long as it isn't a long-running piece of validation.

If you could give more details about what steps are logically involved in your process, that would help.