static void Main(string[] args) { DELG d = new DELG(work.methode); Console.WriteLine(d.Invoke(1, 2).ToString()); Console.Read(); }
static void Main(string[] args) { mutex = new System.Threading.Mutex(false); DELG d = (state) => { string name_thread = (string)state; mutex.WaitOne(); for (int i = 0; i < 3; i++) { ++var; Console.WriteLine("Thread -> {0} -- var -> {1}", name_thread, var.ToString()); System.Threading.Thread.Sleep(2000); } mutex.ReleaseMutex(); }; System.Threading.Thread t1 = new System.Threading.Thread(d.Invoke); System.Threading.Thread t2 = new System.Threading.Thread(d.Invoke); t1.Start(((object)("T1"))); t2.Start(((object)("T2"))); Console.Read(); }
static void Main(string[] args) { semaphore = new System.Threading.Semaphore(2, 2); DELG d = (state) => { semaphore.WaitOne(); for (int i = 0; i < 3; i++) { string name_thread = (string)state; Console.WriteLine("Thread -> {0} -- Etat -> {1}", name_thread, simu_cnx_db.ToString()); System.Threading.Thread.Sleep(2000); } semaphore.Release(); }; System.Threading.Thread t1 = new System.Threading.Thread(d.Invoke); System.Threading.Thread t2 = new System.Threading.Thread(d.Invoke); System.Threading.Thread t3 = new System.Threading.Thread(d.Invoke); t1.Start(((object)("T1"))); t2.Start(((object)("T2"))); t3.Start(((object)("T3"))); Console.Read(); }
static void Main(string[] args) { DELG d = x => x * x; Console.WriteLine(d.Invoke(4).ToString()); Console.Read(); }
static void Main(string[] args) { //equivalent (sans delegate) à: //work.method(1,2); DELG d = new DELG(work.Add); var resultat = d.Invoke(1, 2); Console.WriteLine(resultat); Console.Read(); }
static void Main(string[] args) { int k = 0; int j = 0; DELG d = new DELG(work.methode); Console.WriteLine("donnez le 1er nombre :"); k = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("donnez le 2eme nombre :"); j = Convert.ToInt32(Console.ReadLine()); Console.WriteLine((d.Invoke(k, j).ToString())); Console.Read(); }
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) { System.Threading.Thread t1; DELG d = (o) => { short i = 10; string msg = (string)o; while (i > 0) { Console.WriteLine("Work - > {0}::{1}", msg, i); System.Threading.Thread.Sleep(1000); i--; } }; t1 = new System.Threading.Thread(d.Invoke); System.Threading.ThreadPool.QueueUserWorkItem(delegate { t1.Start(((object)("T1"))); }); 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) { semaphore = new Semaphore(2, 2); DELG deleg = (state) => { semaphore.WaitOne(); for (int i = 0; i < 3; i++) { string name_thread = (string)state; Console.WriteLine("Thread -> " + name_thread + " -- Etat -> " + simu_cnx_db.ToString()); Thread.Sleep(2000); } semaphore.Release(); }; Thread t1 = new Thread(deleg.Invoke); Thread t2 = new Thread(deleg.Invoke); Thread t3 = new Thread(deleg.Invoke); t1.Start((object)("T1")); t2.Start((object)("T2")); t3.Start((object)("T3")); Console.Read(); }
static void Main(string[] args) { rwls = new System.Threading.ReaderWriterLockSlim(); DELG Read1 = (state) => { string thread = (string)state; while (true) { rwls.EnterReadLock(); try { Console.WriteLine("Thread -> {0} -- Message -> {1}", thread, val.ToString()); } finally { rwls.ExitReadLock(); } System.Threading.Thread.Sleep(2000); } }; DELG Write1 = (state) => { string thread = (string)state; int[] tb = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 }; for (int i = 0; i < 10; i++) { rwls.EnterWriteLock(); try { val = tb[i]; Console.WriteLine("Changement de val par Thread -> {0}", thread); } finally { rwls.ExitWriteLock(); } System.Threading.Thread.Sleep(3000); } }; DELG Write2 = (state) => { string thread = (string)state; int[] tb = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }; for (int i = 0; i < 10; i++) { rwls.EnterWriteLock(); try { val = tb[i]; Console.WriteLine("Changement de val par Thread -> {0}", thread); } finally { rwls.ExitWriteLock(); } System.Threading.Thread.Sleep(3000); } }; System.Threading.Thread t1 = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Read1.Invoke)); System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Read1.Invoke)); System.Threading.Thread t3 = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Write1.Invoke)); System.Threading.Thread t4 = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Write1.Invoke)); t1.Start((object)("T1")); t2.Start((object)("T2")); t3.Start((object)("T3")); t4.Start((object)("T4")); 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(); }
static void Main(string[] args) { DELG del = new DELG(travail.methode); Console.WriteLine(del(2, 2)); }