Silverlight sequential operation for Asynchronous operation

不打扰是莪最后的温柔 提交于 2019-12-23 02:44:21

问题


I have 3 operation that need to process sequentially, they are

  1. GetMainInformation
  2. GetDetails1
  3. GetDetails2

I'm doing this by code like below but I think it isn't clean. I want to know an alternative way to do async operation in sequential order.

GetMainInformation.Completed += GetDetails1;
GetDetails1.Completed += GetDetails2;

回答1:


You can use Caliburn Micro's IResult and Coroutines. How to ensure all properties have loaded in Silverlight ViewModel pattern (Concurrency Control?)




回答2:


You could create a queue. Something like this:

GetMainInformation.Completed += GetMainInformationCompleted
GetDetails1.Completed += GetDetails1Completed
GetDetails2.Completed += GetDetails2Completed

Start the request:

requestCounter = 3;
GetMainInformationAsync();
GetDetails1();
GetDetails2();

In each one of the completed functions:

void GetMainInformationCompleted()
{
  // Store result in member variable
  requestCounter--;
  if (requestCounter == 0)
  {
    ProcessRequest();
  }
}



回答3:


If the code must run in a specific order, and sequentially, why not have Getdetails2, called in the oncompletion of GetDetails1, etc so it works its way through? Or. Have your service have a function to call all 3, if the data is related, it maybe feasible to have a data structure with lists/arrays within it such as.

   String CustomerName, 
String Address, 
String PhoneNumber, 
List<orders> Orders, 
List<itemsMarked> ItemsMarked

Where the MainInfo goes to the customer parts, the details1 goes into orders and details2 into marked items etc.



来源:https://stackoverflow.com/questions/6288391/silverlight-sequential-operation-for-asynchronous-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!