"exercice thread en c#" Code Answer's

You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "exercice thread en c#" answered properly. Developers are finding an appropriate answer about exercice thread en c# related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like exercice thread en c#. Enter your desired code related query in the search bar and get every piece of information about C# code related question on exercice thread en c#. 

exercice thread en c#

By Elegant ElkElegant Elk on Nov 27, 2020
        static void Main(string[] args)
        {
            Task t = RunAsync();
            t.Wait();
            Console.In.ReadLine();
        }
        public static async Task RunAsync()
        {
            using (SemaphoreSlim verrouRessource = new SemaphoreSlim(0, 1))// on crée un sémaphore qui ne peut libérer qu'une ressource mais qui la marque comme occupée pour l'instant
            {
                Queue<int> ressourcePartage = new Queue<int>();
                //on prépare les tâches
                List<Task> taches = new List<Task>();
                Console.Out.WriteLine("Start write");
                taches.Add(EnvoieDansLaFile(ressourcePartage, verrouRessource));
                Console.Out.WriteLine("Start read");
                taches.Add(LireLaFile(ressourcePartage, verrouRessource));
                verrouRessource.Release();//on peut commencer
                await Task.WhenAll(taches.ToArray());
            }
        }
        public static async Task EnvoieDansLaFile(Queue<int> file, SemaphoreSlim verrou)
        {
            for (int i = 0; i < 15; i++)
            {
                await verrou.WaitAsync();//on attendq que la ressource soit libre
                file.Enqueue(i);//on utilise la ressource
                verrou.Release();//on dit qu'on a finit d'utiliser la ressource pour l'instant
                System.Console.Out.WriteLine("On a ajouté " + i.ToString());
                Thread.Sleep(100);//on attend histoire que l'exemple soit utile
            }
        }

        public static async Task LireLaFile(Queue<int> file, SemaphoreSlim verrou)
        {
            bool first = true;
            await verrou.WaitAsync();//on va utiliser la ressource
            while (file.Count > 0 || first)
            {
                // pour éviter le cas où cette tâche serait exécutée avant la première fois où on met un entier
                if (file.Count > 0 && first)
                {
                    first = false;
                }
                System.Console.Out.WriteLine(file.Dequeue());
                verrou.Release();//on enlève le verrou
                System.Console.Out.WriteLine("on attend le prochain tour");
                Thread.Sleep(150);
                await verrou.WaitAsync();//on va utiliser la ressource au prochain tour de boucle
            }
        }

Source: zestedesavoir.com

Add Comment

0

All those coders who are working on the C# based application and are stuck on exercice thread en c# can get a collection of related answers to their query. Programmers need to enter their query on exercice thread en c# related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about exercice thread en c# for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of exercice thread en c# while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "exercice thread en c#". Visit this developer's friendly online web community, CodeProZone, and get your queries like exercice thread en c# resolved professionally and stay updated to the latest C# updates. 

C# answers related to "exercice thread en c#"

View All C# queries

C# queries related to "exercice thread en c#"

Browse Other Code Languages

CodeProZone