quoted OP's code, fixed indentation
Source Link
t3chb0t
  • 43.6k
  • 8
  • 76
  • 165
            Task.Run(async () =>
            {
                await SendRequests(cookie, request1, request2);
            })
Task.Run(async () =>
{
    await SendRequests(cookie, request1, request2);
})

is a very long-winded way of writing

            SendRequests(cookie, request1, request2)

.GetAwaiter().GetResult() is also a long-winded way of writing .Result, but since you're ignoring the return value it's probably necessary.


Can the SendRequests method ever return before the requests finish / get resolved?

Of course. Otherwise it would return List<HttpResponseMessage> rather than Task<List<HttpResponseMessage>>. However, assuming that wasn't the question you actually intended to ask...

The doc for SendAsync says

The returned task object will complete once the entire response including content is read.

Since you await both of the tasks, the responses must finish before the SendRequests task will finish. (Of course, if the first one throws an exception then the second won't even be sent).

It's not clear to me whether the intention was for the requests to be made in parallel. That seems to be implied by

to 2 different endpoints (not related to or dependent on each other).

In that case you would want to create the tasks and then wait for them both:

                var responseTask1 = httpClient.SendAsync(request1);
                var responseTask2 = httpClient.SendAsync(request2);
                await Task.WhenAll(responseTask1, responseTask2);

                responseMessages.AddRange(new [] { responseTask1.Result, responseTask2.Result });

                return responseMessages;

Although I have to say that I don't understand the purpose of creating responseMessages in the outer scope.

            Task.Run(async () =>
            {
                await SendRequests(cookie, request1, request2);
            })

is a very long-winded way of writing

            SendRequests(cookie, request1, request2)

.GetAwaiter().GetResult() is also a long-winded way of writing .Result, but since you're ignoring the return value it's probably necessary.


Can the SendRequests method ever return before the requests finish / get resolved?

Of course. Otherwise it would return List<HttpResponseMessage> rather than Task<List<HttpResponseMessage>>. However, assuming that wasn't the question you actually intended to ask...

The doc for SendAsync says

The returned task object will complete once the entire response including content is read.

Since you await both of the tasks, the responses must finish before the SendRequests task will finish. (Of course, if the first one throws an exception then the second won't even be sent).

It's not clear to me whether the intention was for the requests to be made in parallel. That seems to be implied by

to 2 different endpoints (not related to or dependent on each other).

In that case you would want to create the tasks and then wait for them both:

                var responseTask1 = httpClient.SendAsync(request1);
                var responseTask2 = httpClient.SendAsync(request2);
                await Task.WhenAll(responseTask1, responseTask2);

                responseMessages.AddRange(new [] { responseTask1.Result, responseTask2.Result });

                return responseMessages;

Although I have to say that I don't understand the purpose of creating responseMessages in the outer scope.

Task.Run(async () =>
{
    await SendRequests(cookie, request1, request2);
})

is a very long-winded way of writing

SendRequests(cookie, request1, request2)

.GetAwaiter().GetResult() is also a long-winded way of writing .Result, but since you're ignoring the return value it's probably necessary.


Can the SendRequests method ever return before the requests finish / get resolved?

Of course. Otherwise it would return List<HttpResponseMessage> rather than Task<List<HttpResponseMessage>>. However, assuming that wasn't the question you actually intended to ask...

The doc for SendAsync says

The returned task object will complete once the entire response including content is read.

Since you await both of the tasks, the responses must finish before the SendRequests task will finish. (Of course, if the first one throws an exception then the second won't even be sent).

It's not clear to me whether the intention was for the requests to be made in parallel. That seems to be implied by

to 2 different endpoints (not related to or dependent on each other).

In that case you would want to create the tasks and then wait for them both:

var responseTask1 = httpClient.SendAsync(request1);
var responseTask2 = httpClient.SendAsync(request2);
await Task.WhenAll(responseTask1, responseTask2);

responseMessages.AddRange(new [] { responseTask1.Result, responseTask2.Result });

return responseMessages;

Although I have to say that I don't understand the purpose of creating responseMessages in the outer scope.

Source Link
Peter Taylor
  • 23.9k
  • 1
  • 44
  • 90

            Task.Run(async () =>
            {
                await SendRequests(cookie, request1, request2);
            })

is a very long-winded way of writing

            SendRequests(cookie, request1, request2)

.GetAwaiter().GetResult() is also a long-winded way of writing .Result, but since you're ignoring the return value it's probably necessary.


Can the SendRequests method ever return before the requests finish / get resolved?

Of course. Otherwise it would return List<HttpResponseMessage> rather than Task<List<HttpResponseMessage>>. However, assuming that wasn't the question you actually intended to ask...

The doc for SendAsync says

The returned task object will complete once the entire response including content is read.

Since you await both of the tasks, the responses must finish before the SendRequests task will finish. (Of course, if the first one throws an exception then the second won't even be sent).

It's not clear to me whether the intention was for the requests to be made in parallel. That seems to be implied by

to 2 different endpoints (not related to or dependent on each other).

In that case you would want to create the tasks and then wait for them both:

                var responseTask1 = httpClient.SendAsync(request1);
                var responseTask2 = httpClient.SendAsync(request2);
                await Task.WhenAll(responseTask1, responseTask2);

                responseMessages.AddRange(new [] { responseTask1.Result, responseTask2.Result });

                return responseMessages;

Although I have to say that I don't understand the purpose of creating responseMessages in the outer scope.