コード例 #1
0
        static void Main(string[] args)
        {
            int threadId = 0;

            RunOnThreadPool poolDelegate = Test;

            var t = new Thread(() => Test(out threadId));

            t.Start();
            t.Join();

            Console.WriteLine("主线程里:线程id: {0}", threadId);

            IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback, "一个异步调用的委托...");


            r.AsyncWaitHandle.WaitOne();

            string result = poolDelegate.EndInvoke(out threadId, r);            //EndInvoke事实上会等待异步操作的完成

            Console.WriteLine("主线程里:线程池工作线程id: {0}", threadId);
            Console.WriteLine(result);

            Thread.Sleep(TimeSpan.FromSeconds(2));

            Console.ReadKey();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            int threadId = 0;
            // 给委托变量赋值
            RunOnThreadPool poolDelegate = Test;

            var t = new Thread(() => { Test(out threadId); });

            t.Start();
            t.Join();
            Console.WriteLine($"1.返回的线程Id:{threadId}");

            // 通过调用委托变量的BeginInvoke方法来运行委托(执行Test方法)
            IAsyncResult ar = poolDelegate.BeginInvoke(out threadId, Callback, "异步委托调用");

            ar.AsyncWaitHandle.WaitOne();
            // 调用委托的EndInvoke会等待异步操作(Test方法)完成
            // 异步操作执行完成之后,开始执行回掉函数;异步操作和回掉函数很可能会被线程池中同一个工作线程执行
            string result = poolDelegate.EndInvoke(out threadId, ar);

            Console.WriteLine($"2.返回的线程Id:{threadId}");
            Console.WriteLine($"返回值:{result}");

            Console.ReadKey();
        }
コード例 #3
0
        //TODO:unsupport platform
        public static void Perfom()
        {
            int threadId = 0;

            RunOnThreadPool poolDelegate = Test;

            var t = new Thread(() =>
            {
                Test(out threadId);
            });

            t.Start();
            t.Join();

            WriteLine($"Thread Id:{threadId}");

            IAsyncResult r = poolDelegate.BeginInvoke(out threadId, CallBack, "a delegate async call");

            string res = poolDelegate.EndInvoke(out threadId, r);

            WriteLine($"Thread pool worker thread Id:{threadId}");
            WriteLine(res);

            Sleep(TimeSpan.FromSeconds(2));
        }
コード例 #4
0
        static void Main(string[] args)
        {
            int threadId = 0;

            RunOnThreadPool poolDelegate = Test;

            // 不使用异步处理
            var t = new Thread(() => Test(out threadId));

            t.Start();
            // 等待t结束
            t.Join();
            // 打印结果
            Console.WriteLine($"线程Id:{threadId}");

            // 开始异步处理,并指定回调函数
            IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback, "一个代理异步调用");

            // 等待poolDelegate执行结束
            r.AsyncWaitHandle.WaitOne();
            // 获取异步处理结果
            // 虽然threadId是按址传参,但也必须通过EndInvoke获取返回结果
            string result = poolDelegate.EndInvoke(out threadId, r);

            // 打印结果
            Console.WriteLine($"线程池工作线程Id:{threadId}");
            Console.WriteLine(result);
            // 这里的延迟是为了给回调函数足够的执行时间
            Thread.Sleep(TimeSpan.FromSeconds(2));
        }
コード例 #5
0
        public void TestMain()
        {
            int             threadId     = 0;
            RunOnThreadPool poolDelegate = Test1;
            var             t            = new Thread(() => Test1(out threadId));

            t.Start();
            t.Join();
            WriteLine($"Thread id:{threadId}");
            IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback, "a delegate asynchronous call");

            r.AsyncWaitHandle.WaitOne();
            string result = poolDelegate.EndInvoke(out threadId, r);

            WriteLine($"Thread pool worker thread id:{threadId}");
            Console.WriteLine(result);
            Thread.Sleep(TimeSpan.FromSeconds(2));
            Console.WriteLine();
        }
コード例 #6
0
ファイル: ThreadTools.cs プロジェクト: xeekst/C-
        public static void TestThreadPool()
        {
            int             threadId     = 0;
            RunOnThreadPool poolDelegate = Test;
            var             t            = new Thread(() => Test(out threadId));

            t.Start();
            t.Join();

            Console.WriteLine("Thread id:{0}", threadId);
            IAsyncResult r = poolDelegate.BeginInvoke(out threadId, CallBack, "a deletegate call");

            r.AsyncWaitHandle.WaitOne();
            string result = poolDelegate.EndInvoke(out threadId, r);

            Console.WriteLine("Thread pool worker thread id:{0}", threadId);
            Console.WriteLine(result);
            Thread.Sleep(TimeSpan.FromSeconds(2));
        }
コード例 #7
0
        public void ThreadPoolTester()
        {
            var mthread = new Thread(() =>
            {
                int threadId = 0;
                RunOnThreadPool poolDelegate = Test;
                var t = new Thread(() => Test(out threadId));
                t.Start();
                t.Join();
                WriteLine($"Thread id : {threadId}");
                var r = poolDelegate.BeginInvoke(out threadId, Callback, "a delegate asynchronous call");
                r.AsyncWaitHandle.WaitOne();
                string result = poolDelegate.EndInvoke(out threadId, r);
                WriteLine($"Thread pool worker thread id: {threadId}");
                WriteLine(result);
            });

            mthread.Start();
        }
コード例 #8
0
        public static void Run(string[] args)
        {
            int             threadId     = 0;
            RunOnThreadPool poolDelegate = Test;
            var             t            = new Thread(() => Test(out threadId));

            t.Start();
            t.Join();
            Console.WriteLine("main:" + threadId);
            Console.WriteLine("main:" + "---------------------");
            // 异步实际是用线程池进程再运行
            IAsyncResult ar = poolDelegate.BeginInvoke(out threadId, Callback, "nema");

            ar.AsyncWaitHandle.WaitOne();
            string result = poolDelegate.EndInvoke(out threadId, ar);

            Console.WriteLine("main:" + threadId);
            Console.WriteLine("main:" + result);
            Thread.Sleep(TimeSpan.FromSeconds(2));
            Console.ReadKey();
        }
コード例 #9
0
        static void Main(string[] args)
        {
            int             threadId     = 0;
            RunOnThreadPool poolDelegate = Test;
            var             t            = new Thread(() => Test(out threadId));

            t.Start();
            t.Join();

            Console.WriteLine("Thread ID=" + threadId);

            #region NetFramework

            //https://stackoverflow.com/questions/45183294/begininvoke-not-supported-on-net-core-platformnotsupported-exception
            //.net core 平台不支持委托开启线程,会抛异常:Operation is not supported on this platform.

            //IAsyncResult ar = poolDelegate.BeginInvoke(out threadId, Callback, "测试是否可以回调");
            //ar.AsyncWaitHandle.WaitOne();
            //string result = poolDelegate.EndInvoke(out threadId, ar);
            //Console.WriteLine("结果:" + result);

            #endregion

            #region NetCore

            // Task默认使用线程池中的线程
            Task task = Task.Run(() => poolDelegate(out threadId));
            // 回调
            task.ContinueWith((task) => Callback(task)); // Callback方法会在task的异步操作完成后被调用
            task.Wait();

            #endregion

            Console.WriteLine("ID:" + threadId);

            Console.ReadKey();
        }
コード例 #10
0
        static void Main(string[] args)
        {
            int threadId = 0;

            RunOnThreadPool poolDelegate = Test;

            //var t = new Thread(() => Test(out threadId));
            //t.Start();
            //t.Join();
            //Console.WriteLine("Thread id: {0}", threadId);

            IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback, "a delegate asynchronous call");

            //r.AsyncWaitHandle.WaitOne();
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(i);                //线程结束前不阻塞主线程
                Thread.Sleep(TimeSpan.FromMilliseconds(100));
            }

            string result = poolDelegate.EndInvoke(out threadId, r);            //等待线程结束(阻塞),执行回调

            Console.WriteLine("0000");

            Console.WriteLine("....Thread pool worker thread id: {0}", threadId);
            Console.WriteLine("............" + result);
            Console.WriteLine("111111");
            Thread.Sleep(TimeSpan.FromSeconds(1));
            Console.WriteLine("222222");


            Thread.Sleep(TimeSpan.FromSeconds(4));



            Console.ReadLine();
        }
コード例 #11
0
ファイル: ThreadPool.cs プロジェクト: luqc123/CSharpTest
        public void Test1()
        {
            int             threadId     = 0;
            RunOnThreadPool poolDelegate = Test;

            //var t = new Thread(() => Test(out threadId));
            //t.Start();
            //t.Join();
            //WriteLine($"thread id : {threadId}");

            IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback, "a delegate asynchronous call");

            r.AsyncWaitHandle.WaitOne();

            string result = poolDelegate.EndInvoke(out threadId, r);

            WriteLine($"threadpool worker id : {threadId}");
            WriteLine($"Result : {result}");

            WriteLine($"Main Thread id : {CurrentThread.ManagedThreadId}");
            WriteLine($"Main Thread State : {CurrentThread.ThreadState}");
            Sleep(TimeSpan.FromSeconds(2));
            Console.ReadLine();
        }
コード例 #12
0
ファイル: Program.cs プロジェクト: jeremyLJ/lab
        static void Main(string[] args)
        {
            int             threadId     = 0;
            RunOnThreadPool poolDelegate = Test;

            var t = new Thread(() => Test(out threadId));

            t.Start();
            t.Join();
            Console.WriteLine("Thread id: {0}", threadId);

            IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback, "a delegate asynchronous call");
            //r.AsyncWaitHandle.WaitOne();

            string result = poolDelegate.EndInvoke(out threadId, r);

            Console.WriteLine("Thread pool work thread id:{0}", threadId);
            Console.WriteLine(result);

            Thread.Sleep(TimeSpan.FromSeconds(2));

            Console.WriteLine("Done....");
            Console.ReadKey();
        }