My multithreading program works, but doesn't print all ranges. I have 100 numbers; I divide them into 10 ranges. I want do some things on threads such as print range and continue my idea according to these ranges. But these ranges are not all the expected ranges.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Test
{
class Program
{
static int numberCount = 100;
static int numberOfThreads = 10;
static int numberOfFinishedThread = 0;
static void Classifier(int lowIndex, int highIndex)
{
Console.WriteLine("L: " + lowIndex + "\tH: " + highIndex);
numberOfFinishedThread++;
if (numberOfFinishedThread == numberOfThreads)
{
Console.Write("Done. Press any key to exit.");
Console.ReadLine();
}
}
static void Main(string[] args)
{
int lowIndex, highIndex;
for (int i = 0; i < numberOfThreads; i++)
{
lowIndex = (numberCount / numberOfThreads) * i;
highIndex = (numberCount / numberOfThreads) * (i + 1);
Thread thread = new Thread(() => Classifier(lowIndex, highIndex));
thread.Start();
}
}
}
}