示例#1
0
        static void Add(object data)
        {
            Console.WriteLine("ID of thread in Add(): {0}",
                              Thread.CurrentThread.ManagedThreadId);

            AddParams ар = (AddParams)data;

            Console.WriteLine("{0} + {1} is {2}",
                              ар.a, ар.b, ар.a + ар.b);
        }
示例#2
0
        private static void Add(object data)
        {
            if (data is AddParams)
            {
                Console.WriteLine($"ID of thread in Add(): {Thread.CurrentThread.ManagedThreadId}");

                AddParams ap = (AddParams)data;
                Console.WriteLine($"{ap.a} + {ap.b} is {ap.a + ap.b}");
            }
        }
 static void Add(object data)
 {
     if (data is AddParams)
     {
         Console.WriteLine($"Id is thread in Add(): {Thread.CurrentThread.ManagedThreadId}");
         AddParams ap = (AddParams)data;
         Console.WriteLine($"{ap.a} + {ap.b} is {ap.a + ap.b}");
         waitHandle.Set(); //Сообщить другому потоку о том, что работа завершена!
     }
 }
        private static async Task AddAsync()
        {
            Console.WriteLine("***** Adding with Thread objects *****");
            Console.WriteLine("ID of thread in Main: {0}", Thread.CurrentThread.ManagedThreadId);

            var ap = new AddParams(10, 10);

            await Sum(ap);

            Console.WriteLine("Other thread is done!");
        }
示例#5
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.a, ap.b, ap.a + ap.b);
         // Сообщить другому потоку о том, что работа завершена.
         waitHandle.Set();
     }
 }
        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.a, ap.b, ap.a + ap.b);
                waitHandle.Set();
            }
        }
        public static void Add(object data)
        {
            if (data is AddParams)
            {
                Console.WriteLine("ID of thread in Add(): {0}",
                                  Thread.CurrentThread.GetHashCode());

                AddParams ap = (AddParams)data;
                Console.WriteLine("{0} + {1} is {2}",
                                  ap.a, ap.b, ap.a + ap.b);
            }
        }
示例#8
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Adding with Thread objects *****");
            Console.WriteLine("ID of thread in Main(): {0}",
                              Thread.CurrentThread.ManagedThreadId);

            AddParams ap = new AddParams(10, 10);
            Thread    t  = new Thread(new ParameterizedThreadStart(Add));

            t.Start(ap);
            Console.ReadLine();
        }
示例#9
0
        private static async Task AddAsync()
        {
            Console.WriteLine("**** Adding with thread objects ****");
            Console.WriteLine($"ID of thread in Add(): {Thread.CurrentThread.ManagedThreadId}");

            //Make an AddParams object to pass to the secondary thread.
            AddParams ap = new AddParams(10, 10);

            await Sum(ap);

            Console.WriteLine("Other thread is done!");
        }
示例#10
0
        static void Main(string[] args) {
            Console.WriteLine("ID of thread in Main(): {0}", Thread.CurrentThread.ManagedThreadId);
            AddParams ap = new AddParams(10, 11);
            Thread t = new Thread(new ParameterizedThreadStart(Add));
            t.Start(ap);

            //Thread.Sleep(5);
            waitHandle.WaitOne();
            Console.WriteLine("Other thread is done");

            Console.ReadLine();
        }
示例#11
0
        static void Add(object data)
        {
            if (data is AddParams)
            {
                WriteLine($"ID of thread in Add(): {CurrentThread.ManagedThreadId}");

                AddParams ap = (AddParams)data;
                WriteLine($"{ap.a} + {ap.b} is {ap.a + ap.b}");

                // Tell other thread we are done.
                waitHandle.Set();
            }
        }
示例#12
0
        static async Task Sum(object data)
        {
            await Task.Run(() =>
            {
                if (data is AddParams)
                {
                    Console.WriteLine($"ID of thread is {Thread.CurrentThread.ManagedThreadId}");

                    AddParams ap = (AddParams)data;
                    Console.WriteLine($"{ap.a} + {ap.b} = {ap.a + ap.b}");
                }
            });
        }
        static async Task Sum(object data)
        {
            await Task.Run(() =>
            {
                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.a, ap.b, ap.a + ap.b);
                }
            });
        }
示例#14
0
        static void Add(object data)
        {
            if (data is AddParams)
            {
                Console.WriteLine($"ID of thread in Add(): {Thread.CurrentThread.ManagedThreadId}");

                AddParams ap = (AddParams)data;
                Console.WriteLine($"{ap.a} + {ap.b} is {ap.a + ap.b}");

                //Сказать другому потоку что мы закончили
                waitHandle.Set();
            }
        }
示例#15
0
        private static async Task AddAsync()
        {
            Console.WriteLine("***** Adding with Thread objects *****");
            Console.WriteLine("ID of thread in Main(): {0}",
              Thread.CurrentThread.ManagedThreadId);

            AddParams ap = new AddParams(10, 10);
            await Sum(ap);


            Console.WriteLine("Other thread is done!");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Adding with Thread objects");
            Console.WriteLine($"Id of thread in Main(): {Thread.CurrentThread.ManagedThreadId}");
            //Создать объект AddParams для передачи вторичному потоку
            AddParams ap = new AddParams(10, 10);
            Thread    t  = new Thread(new ParameterizedThreadStart(Add));

            t.Start(ap);
            //Ожидать пока не поступит уведомление
            waitHandle.WaitOne();
            Console.WriteLine("Other thread is done!");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("***** Adding with Thread objects *****");
            Console.WriteLine("ID of thread in Main(): {0}",
                              Thread.CurrentThread.GetHashCode());

            AddParams ap = new AddParams(10, 10);
            Thread    t  = new Thread(new ParameterizedThreadStart(Add));

            t.Start(ap);

            // Do other work on Main() here.
            Console.ReadLine();
        }
示例#18
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Adding with Thread objects *****");
            Console.WriteLine("ID of thread in Add(): {0}",
                              Thread.CurrentThread.ManagedThreadId);
            AddParams ap      = new AddParams(10, 20);
            Thread    thread1 = new Thread(new ParameterizedThreadStart(Add));

            thread1.Start(ap);

            // Ожидать, пока не поступит уведомление!
            waitHandle.WaitOne();
            Console.WriteLine("Other thread is done!");
            Console.ReadLine();
        }
示例#19
0
        static void Add(Object data)
        {
            if (data is AddParams)
            {
                Console.WriteLine("ID of thread in Add(): {0}",
                                  Thread.CurrentThread.ManagedThreadId);

                AddParams ap = data as AddParams;
                Console.WriteLine("{0} + {1} is {2}",
                                  ap.a, ap.b, ap.a + ap.b);

                //  通知其他线程,该线程一结束
                waitHandle.Set();
            }
        }
示例#20
0
        private static AutoResetEvent waitHandle = new AutoResetEvent(false); // пункт 1
        static void Main(string[] args)
        {
            Console.WriteLine("***** Adding with Thread objects *****");
            Console.WriteLine($"ID of thread in Main(): {Thread.CurrentThread.ManagedThreadId}");

            //Make an AddParams object to pass to the secondary thread
            AddParams ap = new AddParams(10, 10);
            Thread    t  = new Thread(new ParameterizedThreadStart(Add));

            t.Start(ap);

            // автоматическое ожидаение завершение других потоков.
            waitHandle.WaitOne();
            Console.WriteLine("Other thread is done!");
        }
示例#21
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, 10);
            Thread    t  = new Thread(new ParameterizedThreadStart(Add));

            t.Start(ар);

            // Подождать, пока другой поток завершится.
            Thread.Sleep(5);
            Console.ReadLine();
        }
示例#22
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Adding with Thread objects *****");
            Console.WriteLine("ID of thread in Main(): {0}",
              Thread.CurrentThread.ManagedThreadId);

            AddParams ap = new AddParams(10, 10);
            Thread t = new Thread(new ParameterizedThreadStart(Add));
            t.Start(ap);

            // Wait here until you are notified
            waitHandle.WaitOne();

            Console.WriteLine("Other thread is done!");
            Console.ReadLine();
        }
示例#23
0
        static void Main()
        {
            WriteLine("***** Adding with Thread objects *****");
            WriteLine($"ID of thread in Main(): {CurrentThread.ManagedThreadId}");

            AddParams ap = new AddParams(10, 10);
            Thread    t  = new Thread(new ParameterizedThreadStart(Add));

            t.Start(ap);

            // Wait here until you are notified
            waitHandle.WaitOne();

            WriteLine("Other thread is done!");
            ReadLine();
        }
示例#24
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Adding with Thread objects *****");
            Console.WriteLine("ID of thread in MainO : {0}", Thread.CurrentThread.ManagedThreadId);
            // Создать объект AddParams для передачи вторичному потоку.
            AddParams ар = new AddParams(10, 10);
            Thread    t  = new Thread(new ParameterizedThreadStart(Add));

            t.Start(ар);
            //// Подождать, пока другой поток завершится.
            //Thread.Sleep(5);
            //Console.ReadLine();
            // Ожидать, пока не поступит уведомление!
            waitHandle.WaitOne();
            Console.WriteLine("Other thread is done!");
            Console.ReadLine();
        }
示例#25
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Adding with Thread objects ****");
            Console.WriteLine("ID of thread in Main(): {0}", Thread.CurrentThread.ManagedThreadId);

            AddParams ap = new AddParams(10, 20);

            Thread d = new Thread(new ParameterizedThreadStart(Add));

            d.Start(ap);

            waitHandle.WaitOne();

            Console.WriteLine("Other thread is done");

            Console.ReadLine();
        }
示例#26
0
        private static void UseParameterizedThreadStart()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Adding with Thread objects");
            Console.WriteLine($"ID of thread in Primary: {Thread.CurrentThread.ManagedThreadId}");

            AddParams ap = new AddParams(10, 20);
            Thread    t  = new Thread(new ParameterizedThreadStart(Add));

            t.Start(ap);

            ap = new AddParams(300, 500);
            t  = new Thread(new ParameterizedThreadStart(Add));
            t.Start(ap);

            Thread.Sleep(5000);
        }
示例#27
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Adding with Thread objects *****");
            Console.WriteLine("ID of thread in Main(): {0}",
                              Thread.CurrentThread.ManagedThreadId);
            AddParams ap = new AddParams(10, 10);
            Thread    t  = new Thread(new ParameterizedThreadStart(Add));

            t.Start(ap);

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

            Console.ReadLine();
        }
示例#28
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 ap = new AddParams(10, 10);
            Thread    t  = new Thread(new ParameterizedThreadStart(Add));

            t.Start(ap);

            //  等待,知道收到通知
            waitHandle.WaitOne();
            Console.WriteLine("Other thread is done!");

            Console.ReadLine();
        }
示例#29
0
文件: Program.cs 项目: dakazia/apress
        static void Main(string[] args)
        {
            Console.WriteLine("***** Adding with Thread objects *****");
            Console.WriteLine("ID of thread in Main(): {0}",
                              Thread.CurrentThread.ManagedThreadId);

            // Make an AddParams object to pass to the secondary thread.
            AddParams ap = new AddParams(10, 10);
            Thread    t  = new Thread(new ParameterizedThreadStart(Add));

            //set to background thread
            t.IsBackground = true;
            t.Start(ap);
            // Force a wait to let other thread finish.
            //Thread.Sleep(5);

            //Wait for the wait handle to complete
            //_waitHandle.WaitOne();
            //Console.ReadLine();
        }