当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Web service function await until another web service function is executed

问题描述:

I am new in Async things.I have a web service that return me (in a Windows Phone 8 app) some questions and some answers from DB( 2 functios) When a call the GetQuestions function, I put the question in a ListBox and I want for each question to return me some ansewrs ( GetAnswers method), but I want to be in the listBox like that:

Question1

Answer1

Answer2

...

Question2

Answer1

Answer2

The cod below put me in the ListBox like that

Question1

Question2

Question3

Answer1

Answer2

...

How can I stop GetQuestions method and for each questions to add in the Listbox answers?

 private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)

{

ServiceReference1.Service1SoapClient client = new ServiceReference1.Service1SoapClient();

client.GetQuestionsCompleted += client_GetQuestionsCompleted;

client.GetQuestionsAsync(ID);

}

void client_GetQuestionsCompleted(object sender, ServiceReference1.GetQuestionsCompletedEventArgs e)

{

questions = e.Result.Cast<string>().ToList();

for(int i=0;i<questions.Count;i+=2)

{

Listbox1.Items.Add(questions[i]);

ServiceReference1.Service1SoapClient client2 = new ServiceReference1.Service1SoapClient();

client2.GetAnswersCompleted += client2_GetAnswersCompleted;

client2.GetAnswersAsync(questions[i]);

}

}

void client2_GetAnswersCompleted(object sender, ServiceReference1.GetAnswersCompletedEventArgs e)

{

answers = e.Result.Cast<string>().ToList();

for (int i = 1; i < answers.Count; i += 2)

Listbox1.Items.Add(answers[i]);

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: