Silverlight-sequential execution of multiple asynchronous calls, silverlight asynchronous
If you encounter such a functional requirementSame ServiceIt is called multiple times, but the input parameter must be executed after another execution.
Because silverlight supportsAsynchronous callTherefore, it is impossible to control when a service call is returned. What if the call sequence of the parameter queue is executed?
After thinking for one night, I came up with a compromise.Recursion.
1 ObservableCollection<taskVO> ocTask = new ObservableCollection<taskVO>(); 2 taskVO currentTask; 3 int len = 0,index = 0; 4 private void btn_click(object sender , RoutedEventArgs e){ 5 6 if(ocTask == null) { 7 return; 8 } 9 len = ocTask.Count();
11 currentTask = ocTask[index]; 12 13 index++;14 client.methodAsync(currentTask);15 }16 17 void client_methodCompleted(object sender , CompletedEventArgs e){
21 22 if(index >= len) 23 {24 return;25 }26 currentTask = ocTask[index];27 index++;28 client.methodAsync(currentTask);31 }
OcTask is a parameter queue for multiple asynchronous calls.
In a button event, only the first Task is called asynchronously. In the callback function, the next Task is called asynchronously, and so on until the last parameter is completed, return in Completed and end recursion (this is the final condition that the recursive function learned in the textbook must have. The first time the algorithm in the book is applied to the actual situation, it is better to enable the kaison kaosen ^_^)
PS:
Solving this function does not solve the real asynchronous queue sequence call problem. This feature is special. Multiple asynchronous calls use the same method, which causes recursion to solve this problem.
However, the limitations of recursion also limit that asynchronous queues cannot be too long. Otherwise, recursive stack overflow occurs. Therefore, this problem still needs to be improved.