Example #1
0
        public void Run(RacingThreadsHandling racingThreadsHandling)
        {
            Thread thread1, thread2;

            switch (racingThreadsHandling)
            {
            case RacingThreadsHandling.None:
                thread1 = new Thread(DisplayThread)
                {
                    Name = "thread1"
                };
                thread2 = new Thread(DisplayThread)
                {
                    Name = "thread2"
                };
                thread1.Start();
                thread2.Start();
                break;

            case RacingThreadsHandling.Lock:
                thread1 = new Thread(DisplayThreadWithLock)
                {
                    Name = "thread1"
                };
                thread2 = new Thread(DisplayThreadWithLock)
                {
                    Name = "thread2"
                };
                thread1.Start();
                thread2.Start();
                break;

            case RacingThreadsHandling.AutoResetEvents:
                // boolean argument indicates whether the initial state of the event should be set as signaled
                var thread1LockEvent = new AutoResetEvent(false);
                var thread2LockEvent = new AutoResetEvent(true);
                thread1 = new Thread(DisplayThreadWithAutoResetEvent)
                {
                    Name = "thread1"
                };
                thread2 = new Thread(DisplayThreadWithAutoResetEvent)
                {
                    Name = "thread2"
                };
                thread1.Start(new Tuple <AutoResetEvent, AutoResetEvent>(thread1LockEvent, thread2LockEvent));
                thread2.Start(new Tuple <AutoResetEvent, AutoResetEvent>(thread2LockEvent, thread1LockEvent));
                break;
            }
        }
Example #2
0
 private void RunRacingThreadsExample(RacingThreadsHandling racingThreadsHandling)
 {
     new RacingThreadsExample().Run(racingThreadsHandling);
 }