调用WebService时如何在Silvelight应用程序中进行同步? [英] How to synchronous in Silvelight Application when call WebService?

查看:138
本文介绍了调用WebService时如何在Silvelight应用程序中进行同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

例如,我在这里有一个Silverlight应用程序:

Hi everyone,

Example, I have a Silverlight Application here:

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


结果:
当预期结果为Now.ToString()时,Label1的标题为"abc".

原因:
在Silverlight中,调用WebServices流程是一个异步流程.

调用WebService时如何在Silvelight应用程序中进行同步?

感谢您的帮助.


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.

推荐答案

SIlverlight不支持同步调用.

服务返回后,利用Action委托返回您的主代码.
SIlverlight does not support synchronous calls.

Make use of the Action delegate to come back to your main code once the service returns.


我写了一篇有关如何处理Silverlight的异步特性的文章:

我如何优化Silverlight异步Web服务的使用量 [
I wrote an article about how I handle the asynchronous nature of Silverlight:

How I Optimized my Silverlight Asynchronous Web Service Consumption[^]


可能的一种选择是用途:
IAsyncResult.AsyncWaitHandle [
问候
Espen Harlinn
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


这篇关于调用WebService时如何在Silvelight应用程序中进行同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆