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?