Running httpclient sendasync in task list, only one task running












1















We are downloading a lot of data from a webservice. This is done in discrete calls, with the number of rows in the reply (json) vary from a few thousand to arround half a million. This will take from a couple of seconds to about 90 seconds. We do about 200 calls in one night, some night we do perhaps 500 calls.



After receiving the data I consume the JSON, and bulk insert into SQL Server. This is about 10 times faster than the webrequest.



I've tried to make the webrequests parallel by adding the calls to a task list, and then using Whenany to wait for a task to finish



However when I look at the task list efter WhenAny completes, I have no tasks running.



I have my queries in the list "queries", and I try to add the requests t the task list "Replies"



List<Task<HttpResponseMessage>> Replies = new List<Task<HttpResponseMessage>>();
while (queries.Count > 0 || Replies.Count > 0)
{
while (Replies.Count < 10 && queries.Count > 0)
{
string json = queries.First();
queries.RemoveAt(0);
HttpRequestMessage rMessage = new HttpRequestMessage(HttpMethod.Post, "");
rMessage.Headers.Add("Authorization", Token);
rMessage.Content = new StringContent(json, Encoding.UTF8, "application/json");
Replies.Add(client.SendAsync(rMessage, HttpCompletionOption.ResponseHeadersRead));
}

var finished = await Task.WhenAny(Replies);
int succeeded = Replies.Where(a => a.Status == TaskStatus.RanToCompletion).Count();
int running = Replies.Where(a => a.Status == TaskStatus.Running).Count();
int waitingact = Replies.Where(a => a.Status == TaskStatus.WaitingForActivation).Count();
int waitingrun = Replies.Where(a => a.Status == TaskStatus.WaitingToRun).Count();
Console.WriteLine("Got Reply - {0} succeeded,{1} running,{2} wact,{3} wrun", succeeded, running, waitingact, waitingrun);
Replies.Remove(finished);
}


I would expect there to be at least one task running after WhenAny completes, but there is just on with status "RanToCompletion", and all the other tasks have the status "WaitingForActivation"



I have tried to loo the list and call "Start" on each task, but that is not possible.



All of this is included in an async task, that is called from Main in a console app, it is run as



RunBlock().GetAwaiter().OnCompleted(() => { Console.WriteLine("Done"); });


How do I make the tasks run in parallel?










share|improve this question




















  • 1





    Also, you can replace all of this with a helper method: blogs.msdn.microsoft.com/pfxteam/2012/03/05/…

    – usr
    Jan 20 at 15:04
















1















We are downloading a lot of data from a webservice. This is done in discrete calls, with the number of rows in the reply (json) vary from a few thousand to arround half a million. This will take from a couple of seconds to about 90 seconds. We do about 200 calls in one night, some night we do perhaps 500 calls.



After receiving the data I consume the JSON, and bulk insert into SQL Server. This is about 10 times faster than the webrequest.



I've tried to make the webrequests parallel by adding the calls to a task list, and then using Whenany to wait for a task to finish



However when I look at the task list efter WhenAny completes, I have no tasks running.



I have my queries in the list "queries", and I try to add the requests t the task list "Replies"



List<Task<HttpResponseMessage>> Replies = new List<Task<HttpResponseMessage>>();
while (queries.Count > 0 || Replies.Count > 0)
{
while (Replies.Count < 10 && queries.Count > 0)
{
string json = queries.First();
queries.RemoveAt(0);
HttpRequestMessage rMessage = new HttpRequestMessage(HttpMethod.Post, "");
rMessage.Headers.Add("Authorization", Token);
rMessage.Content = new StringContent(json, Encoding.UTF8, "application/json");
Replies.Add(client.SendAsync(rMessage, HttpCompletionOption.ResponseHeadersRead));
}

var finished = await Task.WhenAny(Replies);
int succeeded = Replies.Where(a => a.Status == TaskStatus.RanToCompletion).Count();
int running = Replies.Where(a => a.Status == TaskStatus.Running).Count();
int waitingact = Replies.Where(a => a.Status == TaskStatus.WaitingForActivation).Count();
int waitingrun = Replies.Where(a => a.Status == TaskStatus.WaitingToRun).Count();
Console.WriteLine("Got Reply - {0} succeeded,{1} running,{2} wact,{3} wrun", succeeded, running, waitingact, waitingrun);
Replies.Remove(finished);
}


I would expect there to be at least one task running after WhenAny completes, but there is just on with status "RanToCompletion", and all the other tasks have the status "WaitingForActivation"



I have tried to loo the list and call "Start" on each task, but that is not possible.



All of this is included in an async task, that is called from Main in a console app, it is run as



RunBlock().GetAwaiter().OnCompleted(() => { Console.WriteLine("Done"); });


How do I make the tasks run in parallel?










share|improve this question




















  • 1





    Also, you can replace all of this with a helper method: blogs.msdn.microsoft.com/pfxteam/2012/03/05/…

    – usr
    Jan 20 at 15:04














1












1








1








We are downloading a lot of data from a webservice. This is done in discrete calls, with the number of rows in the reply (json) vary from a few thousand to arround half a million. This will take from a couple of seconds to about 90 seconds. We do about 200 calls in one night, some night we do perhaps 500 calls.



After receiving the data I consume the JSON, and bulk insert into SQL Server. This is about 10 times faster than the webrequest.



I've tried to make the webrequests parallel by adding the calls to a task list, and then using Whenany to wait for a task to finish



However when I look at the task list efter WhenAny completes, I have no tasks running.



I have my queries in the list "queries", and I try to add the requests t the task list "Replies"



List<Task<HttpResponseMessage>> Replies = new List<Task<HttpResponseMessage>>();
while (queries.Count > 0 || Replies.Count > 0)
{
while (Replies.Count < 10 && queries.Count > 0)
{
string json = queries.First();
queries.RemoveAt(0);
HttpRequestMessage rMessage = new HttpRequestMessage(HttpMethod.Post, "");
rMessage.Headers.Add("Authorization", Token);
rMessage.Content = new StringContent(json, Encoding.UTF8, "application/json");
Replies.Add(client.SendAsync(rMessage, HttpCompletionOption.ResponseHeadersRead));
}

var finished = await Task.WhenAny(Replies);
int succeeded = Replies.Where(a => a.Status == TaskStatus.RanToCompletion).Count();
int running = Replies.Where(a => a.Status == TaskStatus.Running).Count();
int waitingact = Replies.Where(a => a.Status == TaskStatus.WaitingForActivation).Count();
int waitingrun = Replies.Where(a => a.Status == TaskStatus.WaitingToRun).Count();
Console.WriteLine("Got Reply - {0} succeeded,{1} running,{2} wact,{3} wrun", succeeded, running, waitingact, waitingrun);
Replies.Remove(finished);
}


I would expect there to be at least one task running after WhenAny completes, but there is just on with status "RanToCompletion", and all the other tasks have the status "WaitingForActivation"



I have tried to loo the list and call "Start" on each task, but that is not possible.



All of this is included in an async task, that is called from Main in a console app, it is run as



RunBlock().GetAwaiter().OnCompleted(() => { Console.WriteLine("Done"); });


How do I make the tasks run in parallel?










share|improve this question
















We are downloading a lot of data from a webservice. This is done in discrete calls, with the number of rows in the reply (json) vary from a few thousand to arround half a million. This will take from a couple of seconds to about 90 seconds. We do about 200 calls in one night, some night we do perhaps 500 calls.



After receiving the data I consume the JSON, and bulk insert into SQL Server. This is about 10 times faster than the webrequest.



I've tried to make the webrequests parallel by adding the calls to a task list, and then using Whenany to wait for a task to finish



However when I look at the task list efter WhenAny completes, I have no tasks running.



I have my queries in the list "queries", and I try to add the requests t the task list "Replies"



List<Task<HttpResponseMessage>> Replies = new List<Task<HttpResponseMessage>>();
while (queries.Count > 0 || Replies.Count > 0)
{
while (Replies.Count < 10 && queries.Count > 0)
{
string json = queries.First();
queries.RemoveAt(0);
HttpRequestMessage rMessage = new HttpRequestMessage(HttpMethod.Post, "");
rMessage.Headers.Add("Authorization", Token);
rMessage.Content = new StringContent(json, Encoding.UTF8, "application/json");
Replies.Add(client.SendAsync(rMessage, HttpCompletionOption.ResponseHeadersRead));
}

var finished = await Task.WhenAny(Replies);
int succeeded = Replies.Where(a => a.Status == TaskStatus.RanToCompletion).Count();
int running = Replies.Where(a => a.Status == TaskStatus.Running).Count();
int waitingact = Replies.Where(a => a.Status == TaskStatus.WaitingForActivation).Count();
int waitingrun = Replies.Where(a => a.Status == TaskStatus.WaitingToRun).Count();
Console.WriteLine("Got Reply - {0} succeeded,{1} running,{2} wact,{3} wrun", succeeded, running, waitingact, waitingrun);
Replies.Remove(finished);
}


I would expect there to be at least one task running after WhenAny completes, but there is just on with status "RanToCompletion", and all the other tasks have the status "WaitingForActivation"



I have tried to loo the list and call "Start" on each task, but that is not possible.



All of this is included in an async task, that is called from Main in a console app, it is run as



RunBlock().GetAwaiter().OnCompleted(() => { Console.WriteLine("Done"); });


How do I make the tasks run in parallel?







c# multithreading dotnet-httpclient






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 17:08









Wojciech Rak

146111




146111










asked Jan 20 at 14:30









Søren KongstadSøren Kongstad

720414




720414








  • 1





    Also, you can replace all of this with a helper method: blogs.msdn.microsoft.com/pfxteam/2012/03/05/…

    – usr
    Jan 20 at 15:04














  • 1





    Also, you can replace all of this with a helper method: blogs.msdn.microsoft.com/pfxteam/2012/03/05/…

    – usr
    Jan 20 at 15:04








1




1





Also, you can replace all of this with a helper method: blogs.msdn.microsoft.com/pfxteam/2012/03/05/…

– usr
Jan 20 at 15:04





Also, you can replace all of this with a helper method: blogs.msdn.microsoft.com/pfxteam/2012/03/05/…

– usr
Jan 20 at 15:04












1 Answer
1






active

oldest

votes


















1















I would expect there to be at least one task running after WhenAny
completes, but there is just on with status "RanToCompletion", and all
the other tasks have the status "WaitingForActivation"




You're most likely confusing Delegate Tasks with Promise Tasks.



Delegate tasks (thread pool tasks) are created with statuses either Created or WaitForActivation and then through WaitingToRun they changing their status to Running...



Promise tasks (that are generated by async/await) don't have status Running instead from WaitForActivation they change their status directly to RanToCompletion/Faulted/Canceled.



The Tasks are most likely running they just have different status.



You could find this explained very well in an amazing article by Stephen Cleary.






share|improve this answer


























  • Thank you for your answer. I tried sleeping for 10 seconds, and looking at the state of the tasks. No matter how long I wait, a maximum of two tasks will run to completion at a time. As soon as I remove one of them another one completes. Do you why that would be?

    – Søren Kongstad
    Jan 20 at 18:04











  • Try changing Replies.Add(client.SendAsync...) to Replies(Task.Run(client.SendAsync...))

    – Fabjan
    Jan 20 at 18:43






  • 1





    @SørenKongstad 2 sounds like the default HTTP connection limit. Try raising it to int.MaxValue.

    – usr
    Jan 20 at 20:49











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277472%2frunning-httpclient-sendasync-in-task-list-only-one-task-running%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1















I would expect there to be at least one task running after WhenAny
completes, but there is just on with status "RanToCompletion", and all
the other tasks have the status "WaitingForActivation"




You're most likely confusing Delegate Tasks with Promise Tasks.



Delegate tasks (thread pool tasks) are created with statuses either Created or WaitForActivation and then through WaitingToRun they changing their status to Running...



Promise tasks (that are generated by async/await) don't have status Running instead from WaitForActivation they change their status directly to RanToCompletion/Faulted/Canceled.



The Tasks are most likely running they just have different status.



You could find this explained very well in an amazing article by Stephen Cleary.






share|improve this answer


























  • Thank you for your answer. I tried sleeping for 10 seconds, and looking at the state of the tasks. No matter how long I wait, a maximum of two tasks will run to completion at a time. As soon as I remove one of them another one completes. Do you why that would be?

    – Søren Kongstad
    Jan 20 at 18:04











  • Try changing Replies.Add(client.SendAsync...) to Replies(Task.Run(client.SendAsync...))

    – Fabjan
    Jan 20 at 18:43






  • 1





    @SørenKongstad 2 sounds like the default HTTP connection limit. Try raising it to int.MaxValue.

    – usr
    Jan 20 at 20:49
















1















I would expect there to be at least one task running after WhenAny
completes, but there is just on with status "RanToCompletion", and all
the other tasks have the status "WaitingForActivation"




You're most likely confusing Delegate Tasks with Promise Tasks.



Delegate tasks (thread pool tasks) are created with statuses either Created or WaitForActivation and then through WaitingToRun they changing their status to Running...



Promise tasks (that are generated by async/await) don't have status Running instead from WaitForActivation they change their status directly to RanToCompletion/Faulted/Canceled.



The Tasks are most likely running they just have different status.



You could find this explained very well in an amazing article by Stephen Cleary.






share|improve this answer


























  • Thank you for your answer. I tried sleeping for 10 seconds, and looking at the state of the tasks. No matter how long I wait, a maximum of two tasks will run to completion at a time. As soon as I remove one of them another one completes. Do you why that would be?

    – Søren Kongstad
    Jan 20 at 18:04











  • Try changing Replies.Add(client.SendAsync...) to Replies(Task.Run(client.SendAsync...))

    – Fabjan
    Jan 20 at 18:43






  • 1





    @SørenKongstad 2 sounds like the default HTTP connection limit. Try raising it to int.MaxValue.

    – usr
    Jan 20 at 20:49














1












1








1








I would expect there to be at least one task running after WhenAny
completes, but there is just on with status "RanToCompletion", and all
the other tasks have the status "WaitingForActivation"




You're most likely confusing Delegate Tasks with Promise Tasks.



Delegate tasks (thread pool tasks) are created with statuses either Created or WaitForActivation and then through WaitingToRun they changing their status to Running...



Promise tasks (that are generated by async/await) don't have status Running instead from WaitForActivation they change their status directly to RanToCompletion/Faulted/Canceled.



The Tasks are most likely running they just have different status.



You could find this explained very well in an amazing article by Stephen Cleary.






share|improve this answer
















I would expect there to be at least one task running after WhenAny
completes, but there is just on with status "RanToCompletion", and all
the other tasks have the status "WaitingForActivation"




You're most likely confusing Delegate Tasks with Promise Tasks.



Delegate tasks (thread pool tasks) are created with statuses either Created or WaitForActivation and then through WaitingToRun they changing their status to Running...



Promise tasks (that are generated by async/await) don't have status Running instead from WaitForActivation they change their status directly to RanToCompletion/Faulted/Canceled.



The Tasks are most likely running they just have different status.



You could find this explained very well in an amazing article by Stephen Cleary.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 20 at 16:48

























answered Jan 20 at 15:13









FabjanFabjan

10.4k21639




10.4k21639













  • Thank you for your answer. I tried sleeping for 10 seconds, and looking at the state of the tasks. No matter how long I wait, a maximum of two tasks will run to completion at a time. As soon as I remove one of them another one completes. Do you why that would be?

    – Søren Kongstad
    Jan 20 at 18:04











  • Try changing Replies.Add(client.SendAsync...) to Replies(Task.Run(client.SendAsync...))

    – Fabjan
    Jan 20 at 18:43






  • 1





    @SørenKongstad 2 sounds like the default HTTP connection limit. Try raising it to int.MaxValue.

    – usr
    Jan 20 at 20:49



















  • Thank you for your answer. I tried sleeping for 10 seconds, and looking at the state of the tasks. No matter how long I wait, a maximum of two tasks will run to completion at a time. As soon as I remove one of them another one completes. Do you why that would be?

    – Søren Kongstad
    Jan 20 at 18:04











  • Try changing Replies.Add(client.SendAsync...) to Replies(Task.Run(client.SendAsync...))

    – Fabjan
    Jan 20 at 18:43






  • 1





    @SørenKongstad 2 sounds like the default HTTP connection limit. Try raising it to int.MaxValue.

    – usr
    Jan 20 at 20:49

















Thank you for your answer. I tried sleeping for 10 seconds, and looking at the state of the tasks. No matter how long I wait, a maximum of two tasks will run to completion at a time. As soon as I remove one of them another one completes. Do you why that would be?

– Søren Kongstad
Jan 20 at 18:04





Thank you for your answer. I tried sleeping for 10 seconds, and looking at the state of the tasks. No matter how long I wait, a maximum of two tasks will run to completion at a time. As soon as I remove one of them another one completes. Do you why that would be?

– Søren Kongstad
Jan 20 at 18:04













Try changing Replies.Add(client.SendAsync...) to Replies(Task.Run(client.SendAsync...))

– Fabjan
Jan 20 at 18:43





Try changing Replies.Add(client.SendAsync...) to Replies(Task.Run(client.SendAsync...))

– Fabjan
Jan 20 at 18:43




1




1





@SørenKongstad 2 sounds like the default HTTP connection limit. Try raising it to int.MaxValue.

– usr
Jan 20 at 20:49





@SørenKongstad 2 sounds like the default HTTP connection limit. Try raising it to int.MaxValue.

– usr
Jan 20 at 20:49




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277472%2frunning-httpclient-sendasync-in-task-list-only-one-task-running%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How fix org.hibernate.TransientPropertyValueException

Updating UILabel text programmatically using a function

Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage