Exemple #1
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Console.WriteLine("Thread Main is Thread#" + Thread.CurrentThread.ManagedThreadId);

            int numThreads = 10;        //This controls how many threads we want to make for testing

            QuickSync quickSync = new QuickSync();

            MyThread[] myThreads = new MyThread[numThreads];
            TestEvent  GUI       = new TestEvent(myThreads);

            GUI.TrackbarVal = numThreads - 1;
            for (int i = 0; i < numThreads; i++)
            {
                myThreads[i] = new MyThread();
                Thread thread = new Thread(delegate()
                {
                    myThreads[i].Start(quickSync);
                });
                thread.Name         = "Thread#" + thread.ManagedThreadId.ToString();
                thread.IsBackground = true;
                thread.Start();
                while (!thread.IsAlive || !quickSync.Sync)
                {
                    Thread.Sleep(1);
                }
                myThreads[i].thread = thread;
                Console.WriteLine(thread.Name + " is alive");
                quickSync.Sync = false;
            }

            #region Event Sibling Subscribing
            // ********* Event Sibling Subscribing *********
            // Just for example, I will link Thread 0 to thread 1, then 1->2,2->3,3->4
            // so when thread 0 receives an event, so will thread 1, 2, 3, and 4. (Noncommutative)
            // Loops are perfectly acceptable and will not result in Eternal Events
            // e.g. 0->1 + 1->0 is OK, or 0->1 + 1->2 + 2->0... No problem.
            if (numThreads > 0)
            {
                myThreads[0].Event.SubscribeMeTo(myThreads[1].Event);
            }
            //Recursively add thread 2
            if (numThreads > 1)
            {
                myThreads[1].Event.SubscribeMeTo(myThreads[2].Event);
            }
            //Recursively add thread 3
            if (numThreads > 2)
            {
                myThreads[2].Event.SubscribeMeTo(myThreads[3].Event);
            }
            //Recursively add thread 4
            if (numThreads > 3)
            {
                myThreads[3].Event.SubscribeMeTo(myThreads[4].Event);
            }
            #endregion

            Application.Run(GUI);
        }