locked
Return All Calls in Sequence After Async RRS feed

  • Question

  • Moved to intended forum....

    In the example below, the goal is to return the processedResponse. The problem I'm having is that the instruction after SendRequestAsync executes before SendRequestAsync returns the data, so the MyPostProcessing executes on an empty data object.

    How can I modify the code below so that the instructions execute in sequence, and the whole QueryAsync returns when everything is done?

    Thanks...

    public ProcessedResponse Query() {
    QueryAsync(); //Task<ProcessedResponse> task = QueryAsync();
    ProcessedResponse processedResponse = task.Result; } async Task<ProcessedResponse> QueryAsync() { ... HttpResponseMessage httpResponseMessage = await httpClient.SendRequestAsync(httpRequestMessage); processedResponse = MyPostProcessing(httpResponseMessage ); return processedResponse; }


    Randy


    Randy

    Thursday, November 21, 2013 8:37 PM

Answers

  • All the responses really are appreciated...

    When I tried using the await keyword on both the call to the QueryAsync method, and again within the QueryAsync method, the program execution seemed to stop on the second await and never execute any other instructions.

    The work-around that seems to work is to make the following changes:

    1. Do not await the call to QueryAsync()
    2. Do await the SendRequestAsync call inside QueryAsync()
    3. Make the QueryAsync signature "async Task QueryAsync() // no return value.
    4. Allow QueryAsync to update a public property when PostProcessing is complete.
    5. Bind the UI to the public property.

    I really do appreciate all the help, as it gave me ideas for different things to try!

    Thanks again!


    Randy

    • Marked as answer by Anne Jing Thursday, November 28, 2013 7:02 AM
    Wednesday, November 27, 2013 8:56 PM

All replies

  • remove the Query Method and use the Queery Async method in your calling code? why want to make it synchrone?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Thursday, November 21, 2013 9:04 PM
  • The code above is pseudo-code. I'm hoping to find a way to prevent MyPostProcessing from running until SendRequestAsync completes.

    All thoughts welcome....


    Randy

    Thursday, November 21, 2013 9:09 PM
  • Hi,

    Change your code like below:

    public async ProcessedResponse Query()
    {
     await QueryAsync();
      //Task<ProcessedResponse> task = QueryAsync();
    ProcessedResponse processedResponse = task.Result;
    }

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    • Edited by Anne Jing Friday, November 22, 2013 7:06 AM edit
    • Proposed as answer by Sachin S Friday, November 22, 2013 3:41 PM
    Friday, November 22, 2013 7:06 AM
  • Many thanks for the response. When I do this, I get a compile error saying the following:

    The return type of an async method must void, Task or Task<T>.

    What I'm really hoping for is to make QueryAsync a truly Async method. That means the following:

    HttpResponseMessage httpResponseMessage = await httpClient.SendRequestAsync(httpRequestMessage);

    //wait until the line above completes, then execute the line below
      processedResponse
    = MyPostProcessing(httpResponseMessage );

    return processedResponse!!!

    All thoughts welcome!


    Randy

    Friday, November 22, 2013 8:30 PM
  • I modified the code as shown below, and yet again, found it does NOT work. I added the ContinueWith, thinking it would do the

    Post Processing AFTER SendRequestAsync returns data. The problem is that after executing QueryAsync(), the status of httpResponseTask = WaitingForActivation.

    So here's the question: How do I activate httpResponseTask?????????????????????

    public ProcessedResponse Query()
    {
      ProcessedResponse processedResponse = null;
    
      HttpResponseMessage httpResponseTask = QueryAsync();
    
      // Need line here to Activate httpResponseTask!!!!!!!!!!!!!!!!!!!!
    
      await httpResponseTask.ContinueWith(t =>
        {
          processedResponse = MyPostProcessing(t.Result);
        )};
      return processedResponse
    }
    
    async HttpResponseMessage QueryAsync()
    {
      ...
      HttpResponseMessage httpResponseMessage = await httpClient.SendRequestAsync(httpRequestMessage);
    
      return httpResponseMessage 
    }
    
    


    Randy

    Sunday, November 24, 2013 6:48 PM
  • Hi,

    You can await the QueryAsync()method, some codes below:

      public async Task<ProcessedResponse> Query()
            {
                ProcessedResponse processedResponse = await QueryAsync();
                return processedResponse;
                
                
            }
        
            async Task<ProcessedResponse> QueryAsync()
    {
        HttpClient httpClient = new HttpClient();
      HttpResponseMessage httpResponseMessage = await httpClient.SendRequestAsync(httpRequestMessage);
      processedResponse = MyPostProcessing(httpResponseMessage );
    
      return processedResponse;
    }

    You can refer to the link to know Asynchronous programming in winrt:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh464924.aspx

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, November 26, 2013 2:57 AM
  • All the responses really are appreciated...

    When I tried using the await keyword on both the call to the QueryAsync method, and again within the QueryAsync method, the program execution seemed to stop on the second await and never execute any other instructions.

    The work-around that seems to work is to make the following changes:

    1. Do not await the call to QueryAsync()
    2. Do await the SendRequestAsync call inside QueryAsync()
    3. Make the QueryAsync signature "async Task QueryAsync() // no return value.
    4. Allow QueryAsync to update a public property when PostProcessing is complete.
    5. Bind the UI to the public property.

    I really do appreciate all the help, as it gave me ideas for different things to try!

    Thanks again!


    Randy

    • Marked as answer by Anne Jing Thursday, November 28, 2013 7:02 AM
    Wednesday, November 27, 2013 8:56 PM