views:

217

answers:

3

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.

+3  A: 

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.

Jon Skeet
I'm validating some basics before sending off the service call, like that the user name is not empty or has invalid characters. You can ignore that part.
CACuzcatlan
+1  A: 

See, Dispatcher actually holds the UI thread. As every control has strong thread affinity, you need to update this using Dispatcher.

From any thread you can access Dispatcher using DispatcherObject.Dispatcher.

this.Dispatcher.Invoke((Action)(()=>{
 .//// your action
});

Now whenever you need to update the elements in the thread, you can wrap around the context to call service anytime. You can use the Background thread or you can call from inside of Dispatcher thread to invoke the Service call.

Calling From dispatcher will hold the UI if the call is synchronous. So I recommend you to do the call from the Background thread just below updating the Invoker.

abhishek
Note that WP7 doesn't have Invoke - only BeginInvoke.
Jon Skeet
Ohh... My bad. :(
abhishek
+1  A: 

All service call are asynchronous in silverlight (and hence windows phone 7) so yes, what you describe is way you do it.

Different service libraries provide different ways to call their methods - some offer a 'call complete' method, others take a event hander passed in, but either way if you want to update the UI (and I assume this includes moving page) you will need to do this on the UI thread, which is what the dispatcher is for.

Dispatcher.BeginInvoke( () => {
   // This code is on the UI thread.
});
samjudson