Carlos Siestrup
Carlos Siestrup

Reputation: 1216

limit number of threads C#

I have to run a loop with many interactions so i thought of using threads, but I dont know how to control the number of thread:

for (int i = 0; i < 45856582; i = i + 10000)
{
    string result = request.Get(String.Format(mold, i));
    Worker temp = new Worker(i, i + 10000);
    Thread th = new Thread( new ThreadStart( temp.DoWork ));
    th.Start();
}

This loop would create too many threads, how can I make it in a way that the loop will stop and wont create more threads than a certain number and wait for one of the created threads to be done in order to continue?

This is the worker class :

public class Worker
{
    int startIndex = 0;
    int endIndex   = 0;

    public Worker(int startIndex , int endIndex)
    {
        this.startIndex = startIndex;
        this.endIndex   = endIndex;
    }

    public void DoWork()
    {

        for (int i = startIndex; i < endIndex; i++)
        {
            // Do Work
        }
    }
}

Upvotes: 0

Views: 4835

Answers (2)

SynerCoder
SynerCoder

Reputation: 12766

I would suggest you change your entire code to use the Parallel class:

https://msdn.microsoft.com/en-us/library/dd781401(v=vs.110).aspx

// Options to the set maximum number of threads. 
// This is not necessary, .NET will try to use the best amount of cores there is. (as pointed out by Panagiotis Kanavos)
// Overload of method is available without the options parameter
var options = new ParallelOptions()
{
    MaxDegreeOfParallelism = 4 // maximum number of threads
};

const int batchSize = 1000;
int totalSize = 45856582;
int loops = totalSize / batchSize;

// if there is a rest in the division (in this case 582)
// then add an extra loop
if(totalSize % batchSize != 0) 
    loops++;

// The body on the loop
Action<int> loopBody = i =>
{
    Worker temp = new Worker(i * batchSize, i * batchSize + batchSize);
    temp.DoWork();
};

// The result. Check for loopResult.IsCompleted if everything went correctly
var loopResult = Parallel.For(0, loops, options, loopBody);

Or you could use an overload of the method to support cancelling etc.

Upvotes: 6

Andrew Monks
Andrew Monks

Reputation: 666

Try Parallel.For : https://msdn.microsoft.com/en-us/library/dd460713(v=vs.110).aspx

ParallelOptions options = new ParallelOptions();
options.MaxDegreeOfParallelism = 10; //max threads

Parallel.For(0, 45856582, options, i => { 
    Worker temp = new Worker(i, i + 10000);
    temp.DoWork();
} );

Upvotes: 2

Related Questions