How I can implement sync calls to WCF services in SIlverlight?

前端 未结 4 1426
北荒
北荒 2020-12-11 12:07

Sometimes I need to call WCF service in Silverlight and block UI until it returns. Sure I can do it in three steps:

  1. Setup handlers and block UI
  2. Call s
4条回答
  •  醉梦人生
    2020-12-11 12:35

    I'd disagree with Marc there are genuine cases where you need to do synchronous web service calls. However what you probably should avoid is blocking on the UI thread as that creates a very bad user experience.

    A very simple way to implement a service call synchronously is to use a ManualResetEvent.

    ManualResetEvent m_svcMRE = new ManualResetEvent(false);
    MyServiceClient m_svcProxy = new MyServiceClient(binding, address);
    m_svcProxy.DoSomethingCompleted += (sender, args) => {  m_svcMRE.Set(); };
    
    public void DoSomething()
    {
        m_svcMRE.Reset();
        m_svcProxy.DoSomething();
        m_svcMRE.WaitOne();
    }
    

提交回复
热议问题