简体   繁体   中英

Synchronization among asynch threads in silverlight CRM 2011

I am using a silverlight application to develop a webresource. in which i am using ServiceProxy.BeginExecute method which is Asynch operation but. now i am in situation in which i require to call a Method A which internally calls method B which calls CRM Service's Beginexecute, in which it gives the Delegate to execute when completed the BeginExecute method. now since BeginExecute method is Asynch my main thread returns before response come back. i want to hold the main thread until the BeginExecute compltes.

How can i perform this??

While it is not normally suggested to make calls to the web service in a Synchronous fasion, there are exceptions. This is not easy, but there are a couple ways to do this: SynchronousSilverlight

You basically have to write your own ChannelFactory.

We use the reactive framework from Microsoft in our CRM Silverlight development. We only use a tiny portion of it but what we do is make all of our functions that call CRM an IObservable and then subscribe to it to listen for the results. The cool thing is you can chain multiple events together and subscribe to the end result so it will wait until all are done.

Here is an example of a simple call

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 () => { };
            });

        }

And here is how to subscribe to multiple calls

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
                        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM