0

I'm calling an Async Task method inside a for loop like this : -

for(int event_dbID = get_maxID_old + 1 ; event_dbID <= get_maxID_new ; event_dbID++)
{
    SqlCommand command3 = new SqlCommand(queryString3, connection);
    command3.Parameters.Add("@ID", (int)event_dbID);
    SqlDataReader reader3 = command3.ExecuteReader();
    while (reader3.Read())
    {                   
       c8y_Event_MSG = Convert.ToString(reader3[0]);
       c8y_pushEvent(c8y_Event_MSG); //calling async method     
     }
    reader3.Close();
}

for loop can run for hundreds of iteration depending on situation.

A JSON String is passed as an argument to the async Task method. Async Task Method looks like this : -

public async Task c8y_pushEvent(string Event_MSG)
{
//Doing bunch of operations on Event_MSG
    using (var client = new HttpClient())
    {
      //Making a get request using a value from a key in Event_MSG
      //Storing response of get request
      HttpResponseMessage get_response = await client.GetAsync("https://myurl.com/"+controllerName);
      var get_responseString = await get_response.Content.ReadAsStringAsync();

      //Doing bunch of operations on get_responseString
      //Making a Post request passing this get_responseString as Content-Message in stringContent
      HttpResponseMessage response = await client.PostAsync("https://myurl.com", stringContent);
      var responseString = await response.Content.ReadAsStringAsync();
   }
}

Now, what's happening is that all Post requests are successful but their sequence is not correct.

For Eg. If the Sequence of data received over Post request should look like : 1,2,3,4,5 It's coming in random order like : 2,3,1,5,4

Will waiting for the Async method to finish completely before calling it again in c8y_pushEvent(c8y_Event_MSG); make the difference? If yes, then how to wait for it to complete its execution for one string JSON at a time and then pass the next string JSON?

Is it recommended to do so? Will it cause any issues?

  • If you want a sequenced iteration, do you really want an async method? – MX313 May 25 '22 at 12:00
  • @MX313 I'm fairly new to c# and async/await concept. I read somewhere it's best to use async/await for making http requests. But my main goal here is to make Post request and send data in sequence. – saurabh dhyani May 25 '22 at 12:10

1 Answers1

3

You need to await that method:

await c8y_pushEvent(c8y_Event_MSG); //calling async method

To use await keyword, you need to add async keyword to your parent method.

But if you want to run asynchronous method synchronously, it is a different problem. There are answers already to it on SO.

apocalypse
  • 5,764
  • 9
  • 47
  • 95
  • I'm using .NET Compiler Platform(Roslyn). There's no parent method actually & I don't intend to create one unless it's the only option. Is there any other way we can wait for the async Task method to finish Post requests in sequence? – saurabh dhyani May 25 '22 at 12:16
  • @saurabhdhyani It is hard topic: https://stackoverflow.com/questions/9343594/how-to-call-asynchronous-method-from-synchronous-method-in-c – apocalypse May 25 '22 at 12:54