|
Exemple 14-48 extrait du chapitre Les mécanismes utilisables dans C#
Exemple 14-47< > Exemple 14-49
Cet exemple peut être compilé avec la ligne de commande: csc.exe /target:exe Exemple_14_48.cs Erreurs: 0 Avertissements: 0 Remarque:
Exemple_14_48.cs
using System.Collections;
using System.Threading;
public class Program{
static AutoResetEvent eventProducterDone=new AutoResetEvent(false);
static AutoResetEvent eventConsumerDone =new AutoResetEvent(false);
static int currentFibo;
static void Fibo(){
int i1 = 1;
int i2 = 1;
currentFibo = 0;
// Le producteur enclanche le mécanisme.
eventProducterDone.Set();
while(true) {
// On attend que le consommateur ait fini.
eventConsumerDone.WaitOne();
// On produit.
currentFibo = i1 + i2;
i1 = i2;
i2 = currentFibo;
// On signale que l'on a produit.
eventProducterDone.Set();
}
}
static void Main() {
Thread threadProducteur = new Thread(Fibo);
threadProducteur.Start();
for (int i = 1; i < 10; i++) {
// On attend que le producteur ait fini.
eventProducterDone.WaitOne();
// On consomme.
System.Console.WriteLine(currentFibo);
// On signale que l'on a consommé.
eventConsumerDone.Set();
}
}
}
Copyright Patrick Smacchia 2006 2007
|