Alright, I want to keep this short and precise, I'm trying to find the most efficient way to get a large amount of HttpWebRequests completed but I'm slightly confused, if I have a List of tasks and I use the Task.WaitAll function to await until all tasks have completed, is it overkill to make the Task (Action) in the list also an async task? Maybe I can explain myself better in code:
for (int i = 0; i < 50; i++)
{
taskList.Add(Task.Run(() => DoSomething()));
}
Task.WaitAll(taskList.ToArray());
Ok so pretty simple, I have a for loop that'll run the DoSomething method on available threads within the ThreadPool, now let's take two different looks at how I can implement this DoSomething() function
public void DoSomething()
{
Send off the HttpWebRequest here, with no awaits or any async programming
}
OR
public Task<ResponseData> DoSomething()
{
Write the request here that awaits for responses, streams etc
}
If I am already sending the DoSomething function off to the Task.Run function doesnt that already mean it's running on it's own Thread in the ThreadPool ? meaning making the DoSomething function an actual task and implementing awaits/async functionality within that scope becomes redundant? wouldnt that just waste more resources?
DoSomething()do anything other than make your HTTP request? Do you need for it to do something after the request is done? If all you're doing is the request, then you should use the async request API, return fromDoSomething()theTaskrepresenting the request, and just add that to your list instead of usingTask.Run(). IfDoSomething()has more processing to do after the request, you should do the same, but you canawaitin it. Either way, it's unlikely you needTask.Run(). But who can tell, since you didn't share code.