コード例 #1
0
ファイル: Program.cs プロジェクト: Columbia91/AddWithThreads
        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}");
            }
        }
コード例 #3
0
 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(); //Сообщить другому потоку о том, что работа завершена!
     }
 }
コード例 #4
0
        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();
            }
        }
コード例 #7
0
        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
ファイル: Program.cs プロジェクト: maziesmith/.NetCSharp
        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
ファイル: Program.cs プロジェクト: maziesmith/.NetCSharp
        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
ファイル: Program.cs プロジェクト: usedflax/flaxbox
        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();
        }
コード例 #16
0
        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();
        }
コード例 #17
0
        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
ファイル: Program.cs プロジェクト: Columbia91/AddWithThreads
        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
ファイル: Program.cs プロジェクト: usedflax/flaxbox
        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
ファイル: Program.cs プロジェクト: bymagka/MyTroelsen
        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
ファイル: Program.cs プロジェクト: tijokjoy/exercises
        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();
        }