problem in silverlight 4 async how to wait till result come
Here is what i have problem
i have following code :
//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();
开发者_JS百科 subNode.Icon = "/Images/Number/" + iNumber.ToString().Trim() + ".gif";
iNumber++;
this.tvMainNode.Nodes.Add(subNode);
}
here is async method calling wcf service
/// <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);
}
problem : when ever system run --> QuestVM.getExamsMasterbyExamID(eUtility.ConvertInt32(this.txtID.Text), ref entryE_QuestMaster);
its does not wait till i get e.result its just move to next line of code which is foreach loop.
plssss help any one or give idea with sample code what should i do to wait till e.result
i wanted to some how wait till i get e.result
any idea ?
one simple solution. Move the code that you want to execute after getting e.result in this loop.
client.getExamsMasterCompleted+=(s,e)=> { iCollectionDataResult = e.Result; };
Add an Action to the getExamsMasterbyExamID method, then execute the callback after is completed.
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);
}
now when you call the Async method add the loop inside the callback like this:
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);
}
});
after 2 months prob this wont be useful for the asker but maybe for someone else...
精彩评论