Création d’un Thread d’exécution

Author:


{filelink=17386}

using System;
using System.Threading;

namespace sakoba
{
     public class ExempleMultiThread
    {
        public static void Main()
        {
            Console.WriteLine("Lancement de Thread principal.");

            // Création d'un objet Thread
            MonThread objetThread = new MonThread("Mon Thread #1");

            // Création d'un Thread à partir d'un objet.
            Thread objetThreads = new Thread(new ThreadStart(objetThread.run));

            // Finally, start execution of the thread.
            objetThreads.Start();

            do
            {
                Console.Write(".");
                Thread.Sleep(100);
            } while (objetThread.compteur != 10);

            Console.WriteLine("Fin de Thread principal.");
        }
    }  

    class MonThread
    {

        public MonThread(string nom)
        {
            compteur = 0;
            nomThread = nom;
        }

        public void run()
        {

            Console.WriteLine(nomThread + " Lancé.");

            do
            {
                Thread.Sleep(1000);

                Console.WriteLine("Competeur= " +compteur  +
                                  ", dans le Thread " + nomThread);
                compteur++;
            } while (compteur < 10);

            Console.WriteLine(nomThread + " terminé.");
        }
        public int compteur;
        string nomThread;
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *