Exemplo n.º 1
0
 static void Add(object data)
 {
     if (data is AddParams)
     {
         Console.WriteLine("ID of thread in Add(): {0}",
                           Thread.CurrentThread.ManagedThreadId);
         AddParams ap = (AddParams)data;
         Console.WriteLine("{0} + {1} is {2}",
                           ap.number1, ap.number2, ap.number1 + ap.number2);
         Thread.Sleep(5000);
     }
 }
        static void Add(object data)
        {
            if (data is AddParams)
            {
                Console.WriteLine("ID of thread in Add(): {0}",
                                  Thread.CurrentThread.ManagedThreadId);
                AddParams ap = (AddParams)data;
                Console.WriteLine("{0} + {1} is {2}",
                                  ap.number1, ap.number2, ap.number1 + ap.number2);
                Thread.Sleep(2000);

                // Tell other thread we are done.
                waitHandle.Set();
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Adding with Thread objects *****");
            Console.WriteLine("ID of thread in Main(): {0}",
                              Thread.CurrentThread.ManagedThreadId);

            AddParams addParams = new AddParams(10, 20);
            Thread    thread    = new Thread(new ParameterizedThreadStart(Add));

            //it will shut down immediately does not wait for the other thread to complete
            thread.IsBackground = true;
            thread.Start(addParams);

            Console.WriteLine("Other thread is done");
        }
        static void Main(string[] args)
        {
            Console.WriteLine("***** Adding with Thread objects *****");
            Console.WriteLine("ID of thread in Main(): {0}",
                              Thread.CurrentThread.ManagedThreadId);

            AddParams addParams = new AddParams(10, 20);
            Thread    thread    = new Thread(new ParameterizedThreadStart(Add));

            thread.Start(addParams);

            // Force a wait to let other thread finish.
            //Thread.Sleep(5000);


            // Wait here until you are notified!
            waitHandle.WaitOne();
            Console.WriteLine("Other thread is done");
        }