コード例 #1
0
        /*
         *  Process.Start("");
         *  Process.Start("http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx");
         *
         *  Using Locking Mechanisms - One way to deal with data sharing is mutual exclusion.  Mutal exclusion is implemented in .NET
         *  in several ways: monitors, mutexes, semaphores, reader-writer locks.  Locking is both dangerous and resource-intensive.
         *  Sometimes, you just need to perform simple operations, and you need to make sure that they are atomic.  To solve this
         *  kind of problem,.NET offers a class called Interlocked, defined in the System.Threading namespace.   The Interlocked
         *  class has only static methods, and all represent atomic operations, meaning they will be performed without being
         *  interrupted by the scheduler.
         *
         *  Monitors - Work Only with reference objects, not value objects.
         *
         *  The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section.
         *
         *  If another thread tries to enter a locked code, it will wait, block, until the object is released.
         *
         * The section Threading (C# and Visual Basic) discusses threading.
         *
         * The lock keyword calls Enter at the start of the block and Exit at the end of the block.
         *  A ThreadInterruptedException is thrown if Interrupt interrupts a thread that is waiting to enter a lock statement.
         *
         * In general, avoid locking on a public type, or instances beyond your code's control. The common constructs
         *  lock (this), lock (typeof (MyType)), and lock ("myLock") violate this guideline:
         *
         * •lock (this) is a problem if the instance can be accessed publicly.
         *
         *
         * •lock (typeof (MyType)) is a problem if MyType is publicly accessible.
         *
         *
         * •lock("myLock") is a problem because any other code in the process using the same string, will share the same lock.
         *
         *
         * Best practice is to define a private object to lock on, or a private static object variable to protect data common to all instances
         *
         * Locking only works if you lock EVERYWHERE that shared state is vulnerable.
         * It is easy to forget locking somewhere
         * The logic to avoid deadlocks is hard
         * Locking is good on a small scale, but becomes unmanageable on a larger scale
         * Concurrent collections avoid the locking deadlock problems.
         */


        public static void Menu()
        {
            int x = 0;

            do
            {
                Console.Clear();
                Console.WriteLine(" Threading Menu \n ");
                Console.WriteLine(" 0.  ..");
                Console.WriteLine(" 1.  Parm Thread Main \n ");
                Console.WriteLine(" 2.  Stop Thread \n");
                Console.WriteLine(" 3.  Thread Static Main \n");
                Console.WriteLine(" 4.  Thread Shared Resources 1 \n");
                Console.WriteLine(" 5.  Thread Shared Resources 2 \n");
                Console.WriteLine(" 6.  Thread Shared Resources 3 \n");
                Console.WriteLine(" 7.  Thread Shared Resources 4 \n");
                Console.WriteLine(" 8.  Lock Thread  \n");
                Console.WriteLine(" 9.  Quit            \n\n ");
                Console.Write(" Enter Number to execute Routine ");


                int selection;
                selection = Common.readInt("Enter Number to Execute Routine : ", 0, 9);
                switch (selection)
                {
                case 0:
                    break;

                case 1: ThreadingExamples.ParmThreadMain();
                    Console.ReadKey();
                    break;

                case 2: ThreadingExamples.StopThread();
                    Console.ReadKey();
                    break;

                case 3:
                    ThreadingExamples.ThreadStaticMain();
                    Console.ReadKey();
                    Console.WriteLine("Notice Total is same each time");
                    for (int i = 0; i < 3; i++)
                    {
                        ThreadingExamples.Thread_Shared_Resources1();
                    }
                    Console.ReadKey();

                    break;

                case 4:
                    Console.WriteLine("Notice Total is different each time because of shared resources");
                    for (int i = 0; i < 3; i++)
                    {
                        ThreadingExamples.Thread_Shared_Resources2();
                    }
                    Console.ReadKey();

                    break;

                case 5:
                    Console.WriteLine(" Using Lock with Threads ");
                    for (int i = 0; i < 3; i++)
                    {
                        ThreadingExamples.Thread_Shared_Resources3();
                    }
                    Console.ReadKey();

                    break;

                case 6:
                    Console.WriteLine(" Using Monitor.Enter and Monitor.Exit to Lock  ");
                    for (int i = 0; i < 3; i++)
                    {
                        ThreadingExamples.Thread_Shared_Resources4();
                    }
                    Console.ReadKey();

                    break;

                case 9:
                    Console.WriteLine("Threading with the effects of lock keyword");
                    ThreadingExamples.LockThreadMain();
                    Console.ReadKey();
                    x = 9;
                    break;

                default: Console.WriteLine(" Invalid Number");
                    break;
                }
            } while (x < 9);
        }
コード例 #2
0
 public void MenuOpt0()
 {
     ThreadingExamples.Menu();
     Console.ReadKey();
 }