static void Main(string[] args) { DELG dlG_time = new DELG(time); DELG dlg_multiCast; IAsyncResult asyncR; LOCK = new object(); loop = 0; evt += new EVT(state_display); var an_type = new { msg_pext = "pextension", msg_noext = "noextension" }; System.Threading.Thread thd_timeInvok = new System.Threading.Thread(new System.Threading.ThreadStart(dlG_time.Invoke)); System.Threading.Thread thd_paraExt = new System.Threading.Thread( new System.Threading.ThreadStart(() => { System.Threading.Parallel.For(0, 10, i => { Console.WriteLine(@"{0}", an_type.msg_pext); System.Threading.Thread.Sleep(1000); }); evt((object)an_type.msg_pext); })); System.Threading.Thread thd_noParaExt = new System.Threading.Thread( new System.Threading.ThreadStart(() => { for (int i = 0; i < 10; i++) { Console.WriteLine(@"{0}", an_type.msg_noext); System.Threading.Thread.Sleep(1000); } ; evt((object)an_type.msg_noext); })); asyncR = dlG_time.BeginInvoke((async) => { DELG d = (DELG)((System.Runtime.Remoting.Messaging.AsyncResult)async).AsyncDelegate; d.EndInvoke(async); Console.Write("Fin des traveaux"); }, dlG_time); dlg_multiCast = thd_paraExt.Start; dlg_multiCast += thd_noParaExt.Start; dlg_multiCast.Invoke(); while (!asyncR.IsCompleted) { Console.WriteLine("Travaux en cours..."); System.Threading.Thread.Sleep(5000); } Console.Read(); }
static void Main(string[] args) { Message message = new Message("transmitter", "1", "security", true, "error"); GroupOfProcesses gps1 = new GroupOfProcesses(); GroupOfProcesses gps2 = new GroupOfProcesses(); GroupOfProcesses gps3 = new GroupOfProcesses(); Message newMessage1 = null; Message newMessage2 = null; Message newMessage3 = null; DELG dlg1 = (x) => { newMessage1 = (Message)gps1.LaunchProcess(x).Result; }; DELG dlg2 = (x) => { newMessage2 = (Message)gps2.LaunchProcess(x).Result; }; DELG dlg3 = (x) => { newMessage3 = (Message)gps3.LaunchProcess(x).Result; }; IAsyncResult asyncR1 = dlg1.BeginInvoke((object)message, (asr) => { AsyncResult result = (AsyncResult)asr; DELG d = (DELG)result.AsyncDelegate; d.EndInvoke(asr); Console.WriteLine(newMessage1.Data[0]); }, dlg1); IAsyncResult asyncR2 = dlg2.BeginInvoke((object)message, (asr) => { AsyncResult result = (AsyncResult)asr; DELG d = (DELG)result.AsyncDelegate; d.EndInvoke(asr); Console.WriteLine(newMessage2.Data[0]); }, dlg2); IAsyncResult asyncR3 = dlg3.BeginInvoke((object)message, (asr) => { AsyncResult result = (AsyncResult)asr; DELG d = (DELG)result.AsyncDelegate; d.EndInvoke(asr); Console.WriteLine(newMessage3.Data[0]); }, dlg3); Console.Read(); }
static void Main(string[] args) { IAsyncResult asrr; DELG d = (o) => { short i = 10; string msg = (string)o; while (i > 0) { Console.WriteLine("Work - > {0}::{1}", i, msg); System.Threading.Thread.Sleep(1000); i--; } }; asrr = d.BeginInvoke(((object)("T1")), (async) => { DELG dlg = (DELG)((System.Runtime.Remoting.Messaging.AsyncResult)async).AsyncDelegate; dlg.EndInvoke(async); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("callback"); Console.ForegroundColor = ConsoleColor.Gray; }, d); while (!asrr.IsCompleted) { Console.WriteLine("Le thread principal attend le callback end"); System.Threading.Thread.Sleep(2000); if (asrr.IsCompleted) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("callback end msg"); Console.ForegroundColor = ConsoleColor.Gray; } } Console.Read(); }
static void Main(string[] args) { Dictionary <string, int> scoreTable = new Dictionary <string, int>() { { "Player 1", 0 }, { "Player 2", 0 }, { "Bank", 0 } }; DELG dlG_time = new DELG(time); DELG dlg_multiCast; IAsyncResult asyncR; LOCK = new object(); loop = 0; evt += new EVT(state_display); Thread thd_timeInvok = new Thread(new ThreadStart(dlG_time.Invoke)); Thread thd_game = new Thread(new ThreadStart(() => { Parallel.For(0, 10000, i => { List <Joueur> listJoueurs = new List <Joueur>(); listJoueurs.Add(new Joueur("Player 1", 2)); listJoueurs.Add(new Joueur("Player 2", 2)); listJoueurs.Add(new Joueur("Bank", 3)); Croupier croupier = new Croupier(listJoueurs); croupier.distributerCartes(); foreach (Joueur joueur in listJoueurs) { croupier.calculerScore(joueur); } string winnerName = croupier.designerVainqueur(); scoreTable[winnerName] += 1; Thread.Sleep(10); //Console.WriteLine(""); }); /* * for(int i = 0; i< 10000; i ++) * { * List<Joueur> listJoueurs = new List<Joueur>(); * * listJoueurs.Add(new Joueur("Player 1", 2)); * listJoueurs.Add(new Joueur("Player 2", 2)); * listJoueurs.Add(new Joueur("Bank", 3)); * * Croupier croupier = new Croupier(listJoueurs); * croupier.distributerCartes(); * * foreach (Joueur joueur in listJoueurs) * { * croupier.calculerScore(joueur); * } * * string winnerName = croupier.designerVainqueur(); * scoreTable[winnerName] += 1; * Thread.Sleep(10); * //Console.WriteLine(""); * } */ evt(); })); asyncR = dlG_time.BeginInvoke((async) => { DELG d = (DELG)((System.Runtime.Remoting.Messaging.AsyncResult)async).AsyncDelegate; d.EndInvoke(async); Console.WriteLine("Fin des traveaux"); }, dlG_time); dlg_multiCast = thd_game.Start; dlg_multiCast.Invoke(); while (!asyncR.IsCompleted) // Tant que le résult asyncR n'est pas complété, on affiche un message dans la console toutes les 5 secondes { Console.WriteLine("Travaux en cours..."); Thread.Sleep(5000); } Console.WriteLine("=================================="); Console.WriteLine(""); foreach (var scoreLine in scoreTable) { Console.WriteLine("Le nombre de victoires de " + scoreLine.Key + " est " + scoreLine.Value); } Console.WriteLine(""); Console.WriteLine("=================================="); Console.Read(); }