E
E
eliasum2020-05-21 18:24:04
C++ / C#
eliasum, 2020-05-21 18:24:04

Why doesn't it enter the Count method on the first iteration of the for loop when creating a new thread?

Hello! I study the program in the debugger:

using System;
using System.Threading;

// Синхронизация потоков

class Program
{
    static int x = 0;

    static void Main(string[] args)
    {
        for (int i = 0; i < 5; i++)
        {
            // создаем новый поток (здесь предположение делегата)
            Thread myThread = new Thread(Count);

            // имя нового потока
            myThread.Name = "Поток " + i.ToString();

            // запускаем новый поток
            myThread.Start();
        }

        Console.ReadLine();
    }
    public static void Count()
    {
        x = 1;
        for (int i = 1; i < 9; i++)
        {
            Console.WriteLine("{0}: {1}", Thread.CurrentThread.Name, x);
            x++;
            Thread.Sleep(100);
        }
    }
}


Why does the Compiler not enter the Count() method during the first iteration of the for loop in the Main() method when creating a new thread in the line with code ?
Thread myThread = new Thread(Count);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Pokrovsky, 2020-05-21
@eliasum

What for? He enters it in myThread.Start();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question