Пример #1
0
 public void PrintOdd2(PrintingInfo printInfo)
 {
     for (int i = 0; i < 100; i++)
     {
         if (i % 2 != 0)
         {
             printInfo.ProcessedNumbers++;
             Console.WriteLine(i);
         }
     }
 }
Пример #2
0
 public void Print2(bool isEven, PrintingInfo printInfo)
 {
     try
     {
         if (isEven)
         {
             PrintEven2(printInfo);
         }
         else
         {
             PrintOdd2(printInfo);
         }
     }
     catch (ThreadAbortException ex)
     {
         Console.WriteLine(ex);
     }
 }
Пример #3
0
        // Starting Thread -----------------------------------------------------------------------------
        public void StartThread()
        {
            // Thread t1 = new Thread(PrintEven);
            // t1.Start();
            // PrintOdd();

            /*
             * argument - the old way
             */
            // Thread t1 = new Thread(Print1);
            // t1.Start(false);
            // Print1(true);

            // argument the new way
            var    printInfo = new PrintingInfo();
            Thread t1        = new Thread(() => Print2(false, printInfo));

            /* regularly threads are Foreground threads. need to mark as background */
            t1.IsBackground = true;
            t1.Start();
            if (t1.Join(TimeSpan.FromMilliseconds(50)))
            {
                Console.WriteLine($"Im sure that spawned thread processed taht many: {printInfo.ProcessedNumbers}");
            }
            else
            {
                Console.WriteLine("Time out. Can't process results");
            }

            /*
             * Cancel a Thread
             */
            // Thread.Sleep(10);
            // t1.Abort();  // not supported on this platform


            // Print2(true, printInfo);
        }