I am really not sure if I am misunderstanding something here and I am generally not sure what to look for to possibly find a solution, which is why I am asking it here.
First of all, here is my code:
class Program
{
static void Main(string[] args)
{
int sleepTime = 10000;
const int totalTasksToCreate = 10;
Task[] tasks = new Task[totalTasksToCreate];
for (int i = 0; i < totalTasksToCreate; ++i)
{
tasks[i] = Task.Factory.StartNew(() => TaskAction(sleepTime));
sleepTime /= 2;
}
Task.WaitAll(tasks);
}
private static void TaskAction(int sleepTime)
{
Thread.Sleep(sleepTime);
Console.WriteLine(sleepTime);
Console.Beep();
}
}
It is pretty basic, I just somehow wanted to create A Beeping-Machine that beeps in shorter intervals after each beep (And I wanted to do it with tasks because of reasons).
However, I kept wondering why the Sleep-Time was always almost 0 (or 9 in this case) and I really am unsure as to why this is as I am only halving the Value after creating the Task with the Task-Factory.
Is the int-Parameter passed by Reference? Or has it something to do with the StartNew-Method being async?
I am sorry but I am new working with Tasks and Multi-Threading and I cant seem to find any existing questions to this topic or even any discussions elsewhere where there is the same question.
Thanks in advance