另一个异步调用异步调用不能在silverlight中工作

时间:2012-10-05 07:10:11

标签: wcf silverlight

我有一个UI视图lossreport.xaml,下面的代码是

    LossReportTowGlassServiceClient wcf = new LossReportTowGlassServiceClient();
            wcf.HouseholdSearchCompleted += (o, ev) =>
            {
                string a = errorMessg.ToUpper();
        //Code to work with ev
            };
            wcf.HouseholdSearchAsync(lossDate, txtPolicyNumber.Text, errorMessg);

在service.svc页面

             try
                {
                    policyinq.retrieveHouseHoldPoliciesCompleted += new   retrieveHouseHoldPoliciesCompletedEventHandler(policyinq_retrieveHouseHoldPoliciesCompleted);

                    policyinq.retrieveHouseHoldPoliciesAsync(reqh, searchCriteria, lossdate, true, string.Empty, string.Empty);
                    break;
                }
                catch (Exception ex)
                {
                    Logger.Exceptions("", "HouseholdSearch", ex);
                    errorToSend = "Household error";
                }

     void policyinq_retrieveHouseHoldPoliciesCompleted(object sender,   retrieveHouseHoldPoliciesCompletedEventArgs e)
    {
        {
            if (e.transactionNotification != null && e.transactionNotification.transactionStatus == TransactionState.S)
            {

            }
            else
            {
                ErrorHandling.ErrorSend(e.transactionNotification, "HouseHold");
            }

        };
    }

现在在retrieveHouseHoldPolicies完成之前,HomesSearchCompleted事件被触发。如何让它等待

2 个答案:

答案 0 :(得分:0)

你有一个架构问题,服务不应该调用异步请求,除非你有充分的理由(可能会调用一些并行的东西。只需同步调用你的服务器端代码。

服务入口点获得了自己的处理程序线程,它应该是在服务端启动和结束请求响应过程的人。你所做的是在服务端调用异步方法,使处理请求的线程完成他的工作。所以你要么让这个线程等待或者在他身上执行整个逻辑而不调用异步方法,kapish?

答案 1 :(得分:0)

using System.Threading;
ManualResetEvent _wait = new ManualResetEvent(false);
_wait.Set();//In completed event
_wait.WaitOne();//After the event is completed WaitOne will wait untill the _wait is set with value