예제 #1
0
        public static void Print(bool isEven, PrintingInfo printInfo)
        {
            Console.WriteLine($"Current Thread ID : {Thread.CurrentThread.ManagedThreadId}");

            try
            {
                if (isEven)
                {
                    for (int i = 0; i < 10000; i++)
                    {
                        if (i % 2 == 0)
                        {
                            printInfo.ProcessedNumbers++;
                            Console.WriteLine(i);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < 10000; i++)
                    {
                        if (i % 2 != 0)
                        {
                            printInfo.ProcessedNumbers++;
                            Console.WriteLine(i);
                        }
                    }
                }
            }
            catch (ThreadAbortException ex)
            {
                Console.WriteLine(ex);
            }
        }
예제 #2
0
        public static void CoordinatingThreads()
        {
            var    printInfo = new PrintingInfo();
            Thread t1        = new Thread(() => Print(false, printInfo));

            t1.IsBackground = true;
            t1.Priority     = ThreadPriority.Highest;
            t1.Start();

            if (t1.Join(TimeSpan.FromMilliseconds(500)))
            {
                Console.WriteLine($"I'm sure that spawned thread processed that many: {printInfo.ProcessedNumbers}");
            }
            else
            {
                Console.WriteLine("Timed out.  Can't process results.");
            }



            Print(true, printInfo);
        }