Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone,

Example, I have a Silverlight Application here:
VB
Public data as String = "abc"
Private Sub From_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
  Me.InitControl()
    
  Me.Label1.Text = data
End Sub

Private Sub InitControl()
  AddHandler services.ExeMethodCompleted, AddressOf ExeMethodComplete
  services.ExeMethodAsync(....)
End Sub

Private Sub ExeMethodComplete(ByVal sender As Object, ByVal e As ExeMethodCompletedEventArgs)
  If e.Error Is Nothing Then
    Me.data = e.Result.Nodes(1).Descendants("result").Value
    ''assume return result is Now.ToString()
    '' Me.data = Now.ToString()<code></code>
  End If
End Sub

Result:
Label1's caption is "abc" when expect result is Now.ToString().

Reason:
In Silverlight, call WebServices process is an asynchronous process.

How to synchronous in Silvelight Application when call WebService?

Thanks for helping me.
Posted
Updated 25-Jan-11 22:11pm
v2

SIlverlight does not support synchronous calls.

Make use of the Action delegate to come back to your main code once the service returns.
 
Share this answer
 
I wrote an article about how I handle the asynchronous nature of Silverlight:

How I Optimized my Silverlight Asynchronous Web Service Consumption[^]
 
Share this answer
 
One option, that might be possible, is to use:
IAsyncResult.AsyncWaitHandle[^] - the asynch web/wcf request methods returns an IAsyncResult.

It's possible that this can be used to create a synchronized wrapper for the service reference. I haven't tried doing things this way in SilverLight, but it seems like an option.

If it works, it can certainly speed up development of "prototypes" for SL.

Regards
Espen Harlinn
 
Share this answer
 
Silverlight only supports async calls. You have to deal with it.

A userinterface is typically databound (on top of a ViewModel), so when you set the data in the Model, the ViewModel will detect the changes, and raise it's own property changed notification that will update the view.

You need to get some books (or find online tutorials etc) on the Model/View/ViewModel pattern. I recommend staying away from toolkits until you understand the pattern and how to implement it manually. If not, experience tells me you end up using the features the toolkit provide for advanced cases for the simple things as well - leaving you with a mess that's hard to maintain.

If you only make extremely simple programs you can just update the label directly in the callback, but do not go down this route if you want to do anything even slightly serious.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900