silverlight 4 async中的问题如何等到结果来了

时间:2011-08-28 23:47:58

标签: silverlight asynchronous

这就是我遇到的问题

我有以下代码:

  //Get All master record
        entryE_QuestMaster = new ObservableCollection<E_QuestMaster>();
        QuestVM.getExamsMasterbyExamID(eUtility.ConvertInt32(this.txtID.Text), ref entryE_QuestMaster);
        //

        //Loop to show questions
        int iNumber=1;
        foreach (var oIn in entryE_QuestMaster)
        {
            Node subNode = new Node();
            subNode.Content = oIn.e_Question;
            subNode.Name = "Quest_" + iNumber.ToString().Trim();
            subNode.Tag = oIn.e_QID.ToString();
            subNode.Icon = "/Images/Number/" + iNumber.ToString().Trim() + ".gif";
            iNumber++;
            this.tvMainNode.Nodes.Add(subNode);
        }

这里是调用wcf服务的异步方法

  /// <summary>
    /// 
    /// </summary>
    /// <param name="ID"></param>
    public void getExamsMasterbyExamID(int ID, ref ObservableCollection<E_QuestMaster> iCollectionData)
    {
        ObservableCollection<E_QuestMaster> iCollectionDataResult = iCollectionData;
        eLearningDataServiceClient client = new eLearningDataServiceClient();
        client.getExamsMasterCompleted+=(s,e)=>
            {
                iCollectionDataResult = e.Result;
            };
        client.getExamsMasterAsync(ID);
    }

问题:系统运行时 - &gt; QuestVM.getExamsMasterbyExamID(eUtility.ConvertInt32(this.txtID.Text),ref entryE_QuestMaster);

它不等到我得到e.result它只是移动到下一行代码 foreach循环。

plssss帮助任何人或用示例代码提出想法我该怎么做才能等到e.result

我想要一些等到我得到的结果

任何想法?

2 个答案:

答案 0 :(得分:0)

一个简单的解决方案。在此循环中获取e.result后,移动要执行的代码。

client.getExamsMasterCompleted + =(S,E)=&GT;           {                 iCollectionDataResult = e.Result;             };

答案 1 :(得分:0)

向getExamsMasterbyExamID方法添加一个Action,然后在完成后执行回调。

public void getExamsMasterbyExamID(int ID, ref ObservableCollection<E_QuestMaster> iCollectionData,Action<object> callback)
{
    ObservableCollection<E_QuestMaster> iCollectionDataResult = iCollectionData;
    eLearningDataServiceClient client = new eLearningDataServiceClient();
    client.getExamsMasterCompleted+=(s,e)=>
        {
            iCollectionDataResult = e.Result;
            //the callback will be executed on the calling method
            callback(e.Result);
        };
    client.getExamsMasterAsync(ID);
}

现在当你调用Async方法时,在回调中添加循环,如下所示:

    entryE_QuestMaster = new ObservableCollection<E_QuestMaster>();
    QuestVM.getExamsMasterbyExamID(eUtility.ConvertInt32(this.txtID.Text), ref entryE_QuestMaster,r=>
    {
        int iNumber=1;
       foreach (var oIn in entryE_QuestMaster)
       {
            Node subNode = new Node();
            subNode.Content = oIn.e_Question;
            subNode.Name = "Quest_" + iNumber.ToString().Trim();
            subNode.Tag = oIn.e_QID.ToString();
            subNode.Icon = "/Images/Number/" + iNumber.ToString().Trim() + ".gif";
            iNumber++;
            this.tvMainNode.Nodes.Add(subNode);
      }    
   });
经过2个月的探索,这对提问者来说不会有用,但可能对其他人有用......