Question

I am not a thread expert but I wanted to run a function N times at once in parallel and if needed using more than one core when available.

I currently have the bellow code:

tasks = new List<Task>();
for (int i = 0; i < _runThreads; i++)
{
    tasks.Add(Task.Factory.StartNew(() =>
    {
        GetRSS();
    }, stopAllTasks.Token));
}

GetRSS sample:

private void GetRSS()
{
    while (!queue.IsEmpty)
    {
        int total = 0;
        // dequeue, get url bla bla
        using (WebClient client = new WebClient())
        {
            //some code here get html content
        }
        // parse the rss bla bla
        // get the count of total items found 
        // update total variable with the total

        Interlocked.Add(ref counter, total);
    }
}

As you can see, at the GetRSS code I am updating counter with the total entries per page it gets, as my timer ticks it updates the value of counter to a label so I can see the progress.

From this, I have noticed that the tasks are not working all at once but they are in fact working 1 by 1 as the counter only updates X amount, that is equivalent to a single function running.

So from there I believe that the above code doesn't actually run the tasks in parallel but they seems to run it in sequence.

  • Is that right ?

  • If so, how do I convert my code so it will actually run in parallel ?

Was it helpful?

Solution

Your code should definitely run in parallel. Check out similar question here.

Also, you might face the connection limit, that's why you could think it runs sequentially. You could use configuration to override that:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.net>
        <connectionManagement>
            <add address="*" maxconnection="<some reasonable number here>" />
        </connectionManagement>
    </system.net>
</configuration>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top