0

我正在使用silverlight應用程序來開發web資源。其中我使用的是異步操作的ServiceProxy.BeginExecute方法。現在我處於需要調用內部調用方法B的方法A的情況,該方法調用CRM服務的Beginexecute,在該方法中,委託在完成BeginExecute方法時執行。現在,因爲BeginExecute方法是異步,我的主線程在響應回來之前返回。我想要保持主線程,直到BeginExecute compltes。Silverlight CRM 2011中的異步線程之間的同步

我該如何執行此操作?

+0

簡單的方法:調用新的異步內完成前面的異步調用的。 –

+0

現在我已經採用了這種簡單的方法,但是應該做一些更硝和更乾淨的戰爭來做到這一點。我想找到它。 –

回答

0

雖然通常不建議通過同步方式調用Web服務,但也有例外。這並不容易,但有幾種方法可以做到這一點:SynchronousSilverlight

你基本上必須編寫自己的ChannelFactory。

0

我們在我們的CRM Silverlight開發中使用來自Microsoft的反應框架。我們只使用它的一小部分,但我們所做的是使所有將CRM稱爲IObservable的函數,然後訂閱它來監聽結果。最酷的是你可以將多個事件鏈接在一起並訂閱最終結果,因此它會一直等到所有事情完成。

下面是一個簡單的通話

public static IObservable<ProductGroup> RetrieveProductGroupByProductID(IOrganizationService service, Guid productID) 
     { 
      var res = RXCRMMethods.Retrieve(service, "product", productID, new ColumnSet() { Columns = new ObservableCollection<string> { "py3_productgroup" } }); 

      return Observable.Create<ProductGroup>(observer => 
      { 
       try 
       { 
        res.Subscribe(e => 
        { 
         ProductGroup pg = new ProductGroup 
         { 
          ProductGroupId = e.GetAttributeValue<EntityReference>("py3_productgroup").Id 
         }; 
         observer.OnNext(pg); 
        }, 
         ex => 
         { 
          observer.OnError(ex); 
         }); 
       } 
       catch (Exception ex) 
       { 
        observer.OnError(ex); 
       } 
       return() => { }; 
      }); 

     } 

的例子,在這裏是如何訂閱多個呼叫

var LoadQRY = from MatExResult in MaterialExclusionsFactory.GetMaterialExclusionsForOpportunity(this.Config.CrmService, this.OpportunityID) 
            from result in QuoteLineItemFactory.RetrieveQuoteLineItems(Config.CrmService, this) 
            from crateResult in QuotePackingCrateFactory.GetOneOffCratesForQuote(this.Config.CrmService, this.QuoteId) 
            from StaticResult in QuoteLineItemFactory.RetrieveStaticQuoteLineTypes(this.Config.CrmService,this) 
            select new { MatExResult,result,crateResult,StaticResult }; 

        LoadQRY.Subscribe(LoadResult => 
         { 
          //Do something 
         },ex=> 
         { 
          //Catch errors here 
         }); 
+0

感謝凱文給你的時間和精力。我會嘗試你所建議的方式。 –