Esempio n. 1
0
        static void Main(string[] args)
        {
            AsyncPrinter AP = new AsyncPrinter();

            Thread[] threads = new Thread[10];
            for (int i = 0; i < 10; i++)
            {
                threads[i] = new Thread(AP.CheckPrintMessage);
                threads[i].IsBackground = true;
                threads[i].Start();
            }

            for (int i = 0; i < 100; i++)
            {
                AP.AddMessage($"message {i + 1}");
                Thread.Sleep(100);
            }
            AP.sayThis = true; //testing background threads
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            ;
            Console.WriteLine(MyTimer.GetInstance().GetTime());
            Thread.Sleep(2000);
            Console.WriteLine($"----------------------------------");
            AsyncPrinter  printer = new AsyncPrinter();
            List <Thread> threads = new List <Thread>();

            AsyncPrinter async = new AsyncPrinter();

            new Thread(() => { async.CheckPrintMessage(); }).Start();
            new Thread(() => { async.CheckPrintMessage(); }).Start();
            new Thread(() => { async.CheckPrintMessage(); }).Start();
            new Thread(() => { async.CheckPrintMessage(); }).Start();
            new Thread(() => { async.CheckPrintMessage(); }).Start();
            new Thread(() => { async.CheckPrintMessage(); }).Start();
            new Thread(() => { async.CheckPrintMessage(); }).Start();
            new Thread(() => { async.CheckPrintMessage(); }).Start();
            new Thread(() => { async.CheckPrintMessage(); }).Start();
            new Thread(() => { async.CheckPrintMessage(); }).Start();
            for (int i = 1; i <= 10; i++)
            {
                async.AddMessage(MyTimer.GetInstance().GetTime());
                Thread.Sleep(100);
            }

            for (int i = 0; i < 100; i++)
            {
                Thread Print = new Thread(() => printer.AddMessage(Convert.ToString(DateTime.Now)));
                Print.Start();
                for (int j = 0; j <= 100; j++)
                {
                    // Console.Write("Counting: ");
                    Console.WriteLine(string.Format("{0}", j));
                    Thread.Sleep(3);
                }
            }
            Console.WriteLine("Thread Ninja!!!");


            Console.ReadLine();
        }