Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Thread: Exemple d’utilisation des ‘Mutex’


{filelink=17378}

using System;
using System.Threading;
 
class Telechargement
{
    static Mutex mutex = new Mutex(false);
 
    public static void telechargerFichier(string fichier)
    {
        mutex.WaitOne();
        Console.WriteLine("Telechargement.telechargerFichier - Lancé");
        Console.WriteLine("Telechargement.telechargerFichier - En cours d'exécution");
        for (int i = 0; i < 100; i++)
        {
            Console.Write(fichier);
        }
        Console.WriteLine("Telechargement.telechargerFichier - Terminé");
        mutex.Close();
    }
}
 
class ThreadMutex{
    public static void MethodeThredWorker1()
    {
        Console.WriteLine("Worker de thread #1 - Lancé");
        Telechargement.telechargerFichier("exemple.com/file");
    }
 
    public static void MethodeThredWorker2()
    {
        Console.WriteLine("Worker de thread #2 - Lancé");
        Telechargement.telechargerFichier("exemple.com/file2");
    }
 
    public static void Main() {
        ThreadStart worker1 = new ThreadStart(MethodeThredWorker1);
        ThreadStart worker2 = new ThreadStart(MethodeThredWorker2);
 
        Console.WriteLine("Création de Thread Worker");
 
        Thread th1 = new Thread(worker1);
        Thread th2 = new Thread(worker2);
 
        th1.Start();
        th2.Start();
 
        Console.ReadLine();
    }
}

No comments yet.

Leave a Comment