Beispiel #1
0
 public Thread1(MyEvent m, QuickSync quickSync)
 {
     //MyEvent inherits from Form which inherits from Control which is the key to this whole thing working
     //It is the BeginInvoke method of Control which allows us to marshal objects between threads, without it
     //any event handlers would simply fire in the same thread which they were triggered.
     //We don't want to see this form though so I've moved it off screen and out of the task bar
     myEvent = m;
     myEvent.SomethingHappened += new OnSomethingHappened(myEvent.EventMarshal);
     myEvent.FormBorderStyle    = FormBorderStyle.FixedToolWindow;
     myEvent.ShowInTaskbar      = false;
     myEvent.StartPosition      = FormStartPosition.Manual;
     myEvent.Location           = new System.Drawing.Point(-5000, -5000);
     myEvent.Size = new System.Drawing.Size(1, 1);
     System.Windows.Forms.Application.Idle += new EventHandler(OnApplicationIdle);
     quickSync.Sync = true;
     Application.Run(myEvent);
 }
Beispiel #2
0
 /// <summary>
 /// The method used by a thread starting delegate.
 /// </summary>
 public void Start(QuickSync quickSync)
 {
     //MyEvent inherits from Form which inherits from Control which is
     //the key to this whole thing working. It is the BeginInvoke method
     //of Control which allows us to marshal objects between threads,
     //without it any event handlers would simply fire in the same thread
     //which they were triggered. We don't want to see this form though
     //so I've moved it off screen and out of the task bar
     Event = new MyEvent();
     Event.MyEventSender      = this;
     Event.SomethingHappened += new EventMarshalDel(Event.EventMarshal);
     Event.FormBorderStyle    = FormBorderStyle.FixedToolWindow;
     Event.ShowInTaskbar      = false;
     Event.StartPosition      = FormStartPosition.Manual;
     Event.Location           = new System.Drawing.Point(-10000, -10000);
     Event.Size = new System.Drawing.Size(1, 1);
     System.Windows.Forms.Application.Idle += new EventHandler(OnApplicationIdle);
     quickSync.Sync = true;
     Application.Run(Event);
 }
        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);
        }
Beispiel #4
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();

            MyEvent[] myEvent = new MyEvent[numThreads];
            Thread[]  thread  = new Thread[numThreads];
            TestEvent GUI     = new TestEvent(myEvent);

            GUI.TrackbarVal = numThreads - 1;
            for (int i = 0; i < numThreads; i++)
            {
                thread[i] = new Thread(delegate()
                {
                    new Thread1(myEvent[i] = new MyEvent(), quickSync);
                });
                thread[i].Name         = "Thread#" + thread[i].ManagedThreadId.ToString();
                thread[i].IsBackground = true;
                thread[i].Start();
                while (!thread[i].IsAlive || !quickSync.Sync)
                {
                    Thread.Sleep(1);
                }
                Thread.Sleep(50);     //reduce chance of bad sync anomalies
                Console.WriteLine(thread[i].Name + " is alive");
                quickSync.Sync = false;
            }
            for (int i = 0; i < numThreads; i++)
            {
                // I don't think it's possible for a failure like this anymore
                // but it happened a couple times in my earlier development
                // I haven't seen it since but I left this routine here to detect it
                if (myEvent[i] == null)
                {
                    Console.WriteLine("bad sync anomaly detected on #" + i);
                    Application.Exit();
                }
            }

            #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, and 3. (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)
            {
                myEvent[0].SubscribeMeTo(myEvent[1]);
            }
            //Recursively add thread 2
            if (numThreads > 1)
            {
                myEvent[1].SubscribeMeTo(myEvent[2]);
            }
            //Recursively add thread 3
            if (numThreads > 2)
            {
                myEvent[2].SubscribeMeTo(myEvent[3]);
            }
            //Recursively add thread 4
            if (numThreads > 3)
            {
                myEvent[3].SubscribeMeTo(myEvent[4]);
            }
            #endregion

            Application.Run(GUI);
        }