Ejemplo n.º 1
0
        ///////////////////////////////////////////////////////////////////////////////////////////
        // Method to take timestamps before and after running a (simulated) lengthy operation
        // Uses a StringBuilder to collect results from the operation and display them
        //
        // Creates threads explicitly and launches them to run the operations concurrently
        ///////////////////////////////////////////////////////////////////////////////////////////
        public static void testThreading()
        {
            Console.WriteLine("Threads!");
            DateTime      start = DateTime.Now;
            StringBuilder sb    = new StringBuilder();

            string one   = string.Empty;
            string two   = string.Empty;
            string three = string.Empty;
            Thread t1    = new Thread(() => { one = Threadingous.getStr1(); });

            t1.Start();

            // while first thread is running, carry out other operations
            Thread t2 = new Thread(() => { two = Threadingous.getStr2(); });

            t2.Start();

            // while second thread is running, carry out other operations
            Thread t3 = new Thread(() => { three = Threadingous.getStr3(); });

            t3.Start();

            // wait for threads to complete by joining to current thread
            t1.Join();
            t2.Join();
            t3.Join();
            sb.Append(one);
            sb.Append(two);
            sb.Append(three);
            Console.WriteLine("Done with Threads - Values are: " + sb);
            DateTime finish = DateTime.Now;

            Console.WriteLine("Total Milliseconds = " + finish.Subtract(start).TotalMilliseconds);
        }
Ejemplo n.º 2
0
        ///////////////////////////////////////////////////////////////////////////////////////////
        // Main driver function to invoke methods implementing solutions to the problem given the
        // user selection.
        ///////////////////////////////////////////////////////////////////////////////////////////
        static void Main(string[] args)
        {
            string processSelection = string.Empty;

            while (processSelection != "q")
            {
                processSelection = getProcessSelection();
                Console.WriteLine();
                if (processSelection != "q")
                {
                    Console.WriteLine("------------------------------------");
                    switch (processSelection)
                    {
                    case "1":
                        Synchronous.testSynchronously();
                        break;

                    case "2":
                        Threadingous.testThreading();
                        break;

                    case "3":
                        Threadingous.testThreadingPool();
                        break;

                    case "4":
                        Asynchronous.testWithParallelTasks();
                        break;

                    case "5":
                        Asynchronous.testAsynchronously();
                        Console.WriteLine("Main function sleeping for 8 seconds...");
                        Thread.Sleep(8000);
                        break;
                    }
                }
            }
        }