コード例 #1
0
ファイル: Program.cs プロジェクト: Meowse/student1
        static void Main()
        {
            int[] inputValues = new int[2];

            Program p = new Program();
            Calculator c = new Calculator();

            // Get first numeric value.
            inputValues[0] = p.GetNumericValue();

            // Get second numeric value.
            inputValues[1] = p.GetNumericValue();

            // TODO: Create an instance of the ParameterizedThreadStart delegate
            //      passing in the Add method of the Calculator class.
            ParameterizedThreadStart parameterizedThreadStart = new ParameterizedThreadStart(c.Add);

            // TODO: Declare and create an instance of the secondary thread
            //      passing in the delegate instance.
            Thread thread = new Thread(parameterizedThreadStart);
            //Thread thread = new Thread(c.Add);

            //TODO:  Start the secondary thread passing in the object data.
            thread.Start(inputValues);

            System.Threading.Thread.Sleep(2500);

            Console.WriteLine("\nTotal values: {0}",
                c.TotalValue);

            Console.Write("\nPress any key to end.");
            Console.ReadLine();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Meowse/student1
        static void Main()
        {
            Program p = new Program();
            Calculator c = new Calculator();

            // Get first numeric value.
            c.FirstNumber = p.GetNumericValue();

            // Get second numeric value.
            c.SecondNumber = p.GetNumericValue();

            // TODO: Create an instance of the ThreadStart delegate passing
            //      in the Add method of the Calculator class.
            ThreadStart threadStart = new ThreadStart(c.Add);

            // TODO: Declare and create an instance of the secondary thread
            //      passing in the delegate instance.
            Thread thread = new Thread(threadStart);

            //TODO:  Start the secondary thread.
            thread.Start();

            System.Threading.Thread.Sleep(2500);

            Console.WriteLine("\nTotal values: {0}",
                c.TotalValue);

            Console.Write("\nPress any key to end.");
            Console.ReadLine();
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: Meowse/student1
        static void Main()
        {
            int[] valueInts= new int[2];
            Thread secondaryThread = null;

            Program p = new Program();
            Calculator c = new Calculator();

            // TODO: Declare a thread to reference the current primary thread.
            //      Make the reference name "primaryThread".

            // TODO:  Name the thread just declared.

            // TODO:  Store the thread id of the thread declared above in an int
            //      type variable "threadID".

            // TODO: complete the command below to include two arguments for
            //      the properties of the thread declared above - thread id and name.
            Console.WriteLine(
                "\nThread {0}-{1}: Now beginning execution.",
                );

            // Create an instance of the ParameterizedThreadStart delegate passing
            //      in the Add method of the Calculator class.
            ParameterizedThreadStart ts = new ParameterizedThreadStart(c.Add);

            // Declare and create an instance of the secondary thread
            //      passing in the delegate instance.
            secondaryThread = new Thread(ts);

            //TODO: Name the secondary thread.

            // Get first numeric value.
            valueInts[0] = p.GetNumericValue();

            // Get second numeric value.
            valueInts[1] = p.GetNumericValue();

            //Start the secondary thread passing in the data object.
            secondaryThread.Start(valueInts);

            // TODO: Create a while loop that keeps going as long as
            //      the secondary thread is still going. Include the commented
            //      block of code below in the loop.  After placing the commented
            //      code into the loop, uncomment it.

                //Console.WriteLine(
                //    "\nThread {0}: Still waiting for secondary thread to complete.",
                //    primaryThread.ManagedThreadId);
                //System.Threading.Thread.Sleep(750);

            Console.WriteLine("\nThread {0}: Total values: {1}",
               primaryThread.ManagedThreadId,  c.TotalValue);

            Console.Write("\nPress any key to end.");
            Console.ReadLine();
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: Meowse/student1
        static void Main()
        {
            int[] valueInts= new int[2];
            Thread secondaryThread = null;

            Program p = new Program();

            // Declare a thread to reference the current primary thread,
            //      name it, and place its id into a variable.
            Thread primaryThread = Thread.CurrentThread;
            primaryThread.Name = "The Main Thread";
            int threadID = primaryThread.ManagedThreadId;

            Calculator c = new Calculator();

            Console.WriteLine(
                "\nThread {0}-{1}: Now beginning execution.",
                primaryThread.ManagedThreadId, primaryThread.Name);

            // Create an instance of the ParameterizedThreadStart delegate passing
            //      in the Add method of the Calculator class.
            ParameterizedThreadStart ts = new ParameterizedThreadStart(c.Add);

            // Declare and create an instance of the secondary thread
            //      passing in the delegate instance.
            secondaryThread = new Thread(ts);
            // Name the secondary thread.
            secondaryThread.Name = "The Secondary Thread";

            // Get first numeric value.
            valueInts[0] = p.GetNumericValue();

            // Get second numeric value.
            valueInts[1] = p.GetNumericValue();

            //Start the secondary thread passing in the data object.
            secondaryThread.Start(valueInts);

            //TODO: Join the secondary thread to the primary thread
            //      for two seconds.  If secondary thread does not complete
            //      within 2 seconds, then do the following:
            //      a.  Display a message on the console that the thread is
            //              is still alive and being aborted.
            //      b.  Abort the secondary thread.
            //      c.  Join again for another 2 seconds to make sure thread
            //              is aborted.

            Console.WriteLine("\nThread {0}: Total value: {1}",
               primaryThread.ManagedThreadId,  c.TotalValue);

            Console.Write("\nPress any key to end.");
            Console.ReadLine();
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: Meowse/student1
        static void Main()
        {
            int[] valueInts= new int[2];
            Thread secondaryThread = null;

            Program p = new Program();

            // Declare a thread to reference the current primary thread,
            //      name it, and place its id into a variable.
            Thread primaryThread = Thread.CurrentThread;
            primaryThread.Name = "The Main Thread";
            int threadID = primaryThread.ManagedThreadId;

            Calculator c = new Calculator(primaryThread);

            Console.WriteLine(
                "\nThread {0}-{1}: Now beginning execution.",
                primaryThread.ManagedThreadId, primaryThread.Name);

            // Create an instance of the ParameterizedThreadStart delegate passing
            //      in the Add method of the Calculator class.
            ParameterizedThreadStart ts = new ParameterizedThreadStart(c.Add);

            // Declare and create an instance of the secondary thread
            //      passing in the delegate instance.
            secondaryThread = new Thread(ts);
            // Name the secondary thread.
            secondaryThread.Name = "The Secondary Thread";

            // Get first numeric value.
            valueInts[0] = p.GetNumericValue();

            // Get second numeric value.
            valueInts[1] = p.GetNumericValue();

            //Start the secondary thread passing in the data object.
            secondaryThread.Start(valueInts);

            //TODO:  Put the primary thread to sleep infinitely in a
            //      try block and then use the catch block to catch
            //      the exception raised when threads are interrupted.
            //      NOTE: No code is necessary in the catch block.
            try
            {
                Console.WriteLine("\nThread {0}: Putting main to sleep indefinitely.",
                    primaryThread.ManagedThreadId);
                Thread.Sleep(Timeout.Infinite);
            }
            catch (ThreadInterruptedException)
            {
                // No code needed here.
            }

            Console.WriteLine("\nThread {0}: Total value: {1}",
               primaryThread.ManagedThreadId,  c.TotalValue);

            Console.Write("\nPress any key to end.");
            Console.ReadLine();
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: Meowse/student1
        static void Main()
        {
            int[] valueInts1= new int[2];
            int[] valueInts2 = new int[2];
            Thread secondaryThread1 = null;
            Thread secondaryThread2 = null;

            Program p = new Program();

            // Declare a thread to reference the current primary thread,
            //      name it, and place its id into a variable.
            Thread primaryThread = Thread.CurrentThread;
            primaryThread.Name = "The Main Thread";
            int threadID = primaryThread.ManagedThreadId;

            Console.WriteLine(
                "\nThread {0}-{1}: Now beginning execution.",
                primaryThread.ManagedThreadId, primaryThread.Name);

            // Create two instances of the Calculator for two separate threads.
            Calculator c1 = new Calculator(primaryThread);
            Calculator c2 = new Calculator(primaryThread);

            // Create an two instances of the ParameterizedThreadStart delegate passing
            //      in the Add method of the Calculator class.
            ParameterizedThreadStart ts1 = new ParameterizedThreadStart(c1.Add);
            ParameterizedThreadStart ts2 = new ParameterizedThreadStart(c2.Add);

            //************************************************************
            //  Secondary thread 1: Create and get values

            // Create an instance of the secondary thread
            //      passing in the delegate instance.
            //secondaryThread1 = new Thread(ts1);

            //TODO: Use a lambda expression that calls the c1.Add method in an
            //        anonymous method to create an instance of a thread and
            //        store the reference  in secondaryThread1.

            // Name the secondary thread.
            secondaryThread1.Name = "The Secondary Thread 1";

            Console.WriteLine(
                "\nThread {0}: Please enter two numbers for the first addition: ",
                threadID);

            // Get first numeric value.
            valueInts1[0] = p.GetNumericValue();

            // Get second numeric value.
            valueInts1[1] = p.GetNumericValue();

            //************************************************************
            //  Secondary thread 2: Create and get values

            // Create an instance of the secondary thread
            //      passing in the delegate instance.
            //secondaryThread2 = new Thread(ts2);

            //TODO: Use a lambda expression that calls the c2.Add method in an
            //        anonymous method to create an instance of a thread and
            //        store the reference  in secondaryThread2.

            // Name the secondary thread.
            secondaryThread2.Name = "The Secondary Thread 2";

            Console.WriteLine(
                "\nThread {0}: Please enter two numbers for the second addition: ",
                threadID);

            // Get first numeric value.
            valueInts2[0] = p.GetNumericValue();

            // Get second numeric value.
            valueInts2[1] = p.GetNumericValue();

            //*************************************************************
            // Process both secondary threads concurrently.

            //Start the first secondary thread passing in the data object.
            secondaryThread1.Start(valueInts1);

            //Start the second secondary thread passing in the data object.
            secondaryThread2.Start(valueInts2);

            // Join the primary thread to the first secondary thread.
            JoinThread(threadID, secondaryThread1);

            // Join the primary thread to the second secondary thread.
            JoinThread(threadID, secondaryThread2);

            Console.WriteLine("\nThread {0}: Total from first addition on thread {1}: {2}",
               primaryThread.ManagedThreadId, secondaryThread1.Name, c1.TotalValue);

            Console.WriteLine("\nThread {0}: Total from second addition on thread {1}: {2}",
                primaryThread.ManagedThreadId, secondaryThread2.Name, c2.TotalValue);

            Console.Write("\nPress any key to end.");
            Console.ReadLine();
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: Meowse/student1
        static void Main()
        {
            int[] valueInts1= new int[2];
            int[] valueInts2 = new int[2];
            Thread secondaryThread1 = null;
            Thread secondaryThread2 = null;

            Program p = new Program();

            // Declare a thread to reference the current primary thread,
            //      name it, and place its id into a variable.
            Thread primaryThread = Thread.CurrentThread;
            primaryThread.Name = "The Main Thread";
            int threadID = primaryThread.ManagedThreadId;

            Console.WriteLine(
                "\nThread {0}-{1}: Now beginning execution.",
                primaryThread.ManagedThreadId, primaryThread.Name);

            //Declare and create an instance of the callback delegate.
            CallbackDelegate cd =
                new CallbackDelegate(Program.ThreadDoneCallback);

            // Create two instances of the Calculator for two separate threads.
            Calculator c1 = new Calculator(primaryThread, cd);
            Calculator c2 = new Calculator(primaryThread, cd);

            // Create an two instances of the ParameterizedThreadStart delegate passing
            //      in the Add method of the Calculator class.
            ParameterizedThreadStart ts1 = new ParameterizedThreadStart(c1.Add);
            ParameterizedThreadStart ts2 = new ParameterizedThreadStart(c2.Add);

            //************************************************************
            //  Secondary thread 1: Create and get values

            // Declare and create an instance of the secondary thread
            //      passing in the delegate instance.
            secondaryThread1 = new Thread(ts1);
            // Name the secondary thread.
            secondaryThread1.Name = "The Secondary Thread 1";

            Console.WriteLine(
                "\nThread {0}: Please enter two numbers for the first addition: ",
                threadID);

            // Get first numeric value.
            valueInts1[0] = p.GetNumericValue();

            // Get second numeric value.
            valueInts1[1] = p.GetNumericValue();

            //************************************************************
            //  Secondary thread 2: Create and get values

            // Declare and create an instance of the secondary thread
            //      passing in the delegate instance.
            secondaryThread2 = new Thread(ts2);
            // Name the secondary thread.
            secondaryThread2.Name = "The Secondary Thread 2";

            Console.WriteLine(
                "\nThread {0}: Please enter two numbers for the second addition: ",
                threadID);

            // Get first numeric value.
            valueInts2[0] = p.GetNumericValue();

            // Get second numeric value.
            valueInts2[1] = p.GetNumericValue();

            //*************************************************************
            // Process both secondary threads concurrently.

            //Start the first secondary thread passing in the data object.
            secondaryThread1.Start(valueInts1);

            //Start the second secondary thread passing in the data object.
            secondaryThread2.Start(valueInts2);

            lock (SyncObject.Sync)
            {
                Console.WriteLine
                    ("\n{0}: Waiting for threads to complete.",
                    threadID);
            }

            // Put the primary thread into a wait state. This
            // will wait until the AutoResetEvent object is
            // signaled through a call to Set().
            // TODO: 3. Put the primary thread into a wait state
            //      using the AutoResetEvent object.
            if ( _signalPrimaryThread.WaitOne(10000))
            {
                Console.WriteLine("\nThread {0}: Total from first addition on thread {1}: {2}",
                   primaryThread.ManagedThreadId, secondaryThread1.Name, c1.TotalValue);

                Console.WriteLine("\nThread {0}: Total from second addition on thread {1}: {2}",
                    primaryThread.ManagedThreadId, secondaryThread2.Name, c2.TotalValue);
            }

            Console.Write("\nPress any key to end.");
            Console.ReadLine();
        }
コード例 #8
0
ファイル: Program.cs プロジェクト: Meowse/student1
        static void Main()
        {
            int[] valueInts1= new int[2];
            int[] valueInts2 = new int[2];
            Thread secondaryThread1 = null;
            Thread secondaryThread2 = null;

            Program p = new Program();

            // Declare a thread to reference the current primary thread,
            //      name it, and place its id into a variable.
            Thread primaryThread = Thread.CurrentThread;
            primaryThread.Name = "The Main Thread";
            int threadID = primaryThread.ManagedThreadId;

            Console.WriteLine(
                "\nThread {0}-{1}: Now beginning execution.",
                primaryThread.ManagedThreadId, primaryThread.Name);

            //Declare and create an instance of the callback delegate.
            CallbackDelegate cd =
                new CallbackDelegate(Program.ThreadDoneCallback);

            // Create two instances of the Calculator for two separate threads.
            Calculator c1 = new Calculator(primaryThread, cd);
            Calculator c2 = new Calculator(primaryThread, cd);

            //************************************************************
            //  Get values for first background thread.

            Console.WriteLine(
                "\nThread {0}: Please enter two numbers for the first addition: ",
                threadID);

            // Get first numeric value.
            valueInts1[0] = p.GetNumericValue();

            // Get second numeric value.
            valueInts1[1] = p.GetNumericValue();

            //************************************************************
            //  Secondary thread 2: Create and get values

            Console.WriteLine(
                "\nThread {0}: Please enter two numbers for the second addition: ",
                threadID);

            // Get first numeric value.
            valueInts2[0] = p.GetNumericValue();

            // Get second numeric value.
            valueInts2[1] = p.GetNumericValue();

            //*************************************************************
            // Process both secondary background threads concurrently.

            //Start the first secondary background thread passing in the data object.
            // TODO: Start a background thread to execute the Add method using the c1
            //  reference and passing in the valueInts1 array.  Give an error message
            //  if the queuing of the background thread is not successful.

            //Start the second secondary background thread passing in the data object.
            // TODO: Start a background thread to execute the Add method using the c2
            //  reference and passing in the valueInts2 array.  Give an error message
            //  if the queuing of the background thread is not successful.

            lock (SyncObject.Sync)
            {
                Console.WriteLine
                    ("\n{0}: Waiting for threads to complete.",
                    threadID);
            }

            // Put the primary thread into a wait state. This
            // will wait until the AutoResetEvent object is
            // signaled through a call to Set().
            _signalPrimaryThread.WaitOne();

            Console.WriteLine("\nThread {0}: Total from first addition: {1}",
               primaryThread.ManagedThreadId, c1.TotalValue);

            Console.WriteLine("\nThread {0}: Total from second addition: {1}",
                primaryThread.ManagedThreadId, c2.TotalValue);

            Console.Write("\nPress any key to end.");
            Console.ReadLine();
        }