Silverlight 4 Async wait how to?
I have a problem:
//Get All master record
entryE_QuestMaster = new ObservableCollection<E_QuestMaster>();
//Here I am calling my web service to get data
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);
}
Whenever I call
QuestVM.getExamsMasterbyExamID(eUtility.ConvertInt32(this.txtID.Text), ref entryE_QuestMaster);
It开发者_如何学C runs the following code
public void getExamsMasterbyExamID(int ID, ref ObservableCollection<E_QuestMaster> iCollectionData)
{
ObservableCollection<E_QuestMaster> iCollectionDataResult = iCollectionData;
eLearningDataServiceClient client = new eLearningDataServiceClient();
isSync = true;
client.getExamsMasterCompleted+=(s,e)=>
{
iCollectionDataResult = e.Result;
};
client.getExamsMasterAsync(ID);
}
My problem is I wanted to wait until my e.result comes back in iCollectionDataResult
.
Currently after calling this service the system continues to the next line of code which is in the foreach loop. In this stage my entryE_QuestMaster does not have any record, I just wanted to wait till my result comes back before the loop continues.
After Answering ChrisF
no dought what chrisf said will work for me, but i wanted to do every thing in my MVVM class rather then in form level , here what i have change in my code , i still need your help guys and whated to do some professional code rather then just writing huge code.
i have added these two lines in my MVVM class
public delegate void ShowQuestionTreeView(ObservableCollection<sp_GetQuestMasterbyExamID_Result> iResultQuestMaster);
public event ShowQuestionTreeView ShowQuestionforTreeview;
then in method i have added this
/// <summary>
///
/// </summary>
/// <param name="ID"></param>
public void getExamsMasterbyExamID(int ID, ref ObservableCollection<sp_GetQuestMasterbyExamID_Result> iCollectionData)
{
ObservableCollection<sp_GetQuestMasterbyExamID_Result> iCollectionDataResult = iCollectionData;
eLearningDataServiceClient client = new eLearningDataServiceClient();
client.getExamsMasterbyExamIDCompleted+= (s, e) =>
{
iCollectionDataResult = e.Result;
**ShowQuestionforTreeview(iCollectionDataResult);**
};
client.getExamsMasterbyExamIDAsync(ID);
}
on client end i have done this
//Generate Treeview for question
QuestVM.ShowQuestionforTreeview += new eQuestCreateVM.ShowQuestionTreeView(QuestVM_ShowQuestionforTreeview);
method :
void QuestVM_ShowQuestionforTreeview(ObservableCollection<sp_GetQuestMasterbyExamID_Result> iResultQuestMaster)
{
//Loop to show questions
int iNumber = 1;
foreach (var oIn in iResultQuestMaster)
{
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";
subNode.Title = oIn.e_Question_Text;
iNumber++;
tvCreateQuestion.Nodes[0].Nodes.Add(subNode);
}
}
You need to move this code:
client.getExamsMasterCompleted+=(s,e)=>
{
iCollectionDataResult = e.Result;
};
outside your call to getExamsMasterbyExamID
. This will also mean moving where you declare and initialise the eLearningDataServicesClient
.
The way you have your code set up at the moment means that you are assuming that iCollectionDataResult
will be set when the method returns. However, this is not the case. The method will return before the getExamsMasterCompleted
event is fired.
You will need to organise your code along the following lines:
eLearningDataServiceClient client = new eLearningDataServiceClient();
client.getExamsMasterCompleted += (s,e) =>
{
//Loop to show questions
}
client.getExamsMasterAsync(ID);
Or alternatively bind the display of questions to the ObservableCollection so when it gets set the UI updated automatically.
精彩评论