PDA

View Full Version : سوال: به هم ریختن نتیجه برنامه در اجرای آن با thread



ShahinRad97
سه شنبه 17 اردیبهشت 1398, 15:08 عصر
سلام.
این کد قسمت ساده شده یک برنامه است. (کد در محیط کنسول اپ هستش)
انتظار میره چیزی که چاپ کنه 10 سطر باشه . که 10 سطر هم هست.
انتظار میره که این 10 سطر هر کدوم یه رنج 10تایی متفاوت نشون بده. که نمیده. چرا؟
ممنون میشم راهنمایی بفرمایید.
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();
}

}
}
}