博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
线程学习一
阅读量:6594 次
发布时间:2019-06-24

本文共 3553 字,大约阅读时间需要 11 分钟。

static int TakesAWhile(int data, int ms)        {            Console.WriteLine("TakesAWhile started");            Thread.Sleep(ms);            Console.WriteLine("TakesAWhile completed");            return ++data;        }        public delegate int TakesAWhileDelegate(int data, int ms);        static void Main(string[] args)        {            TakesAWhileDelegate d1 = TakesAWhile;            // polling            IAsyncResult ar = d1.BeginInvoke(1, 3000, null, null);            while (!ar.IsCompleted)            {                // doing something else                Console.Write(".");                Thread.Sleep(500);            }            int result = d1.EndInvoke(ar);            Console.WriteLine("result: {0}", result);            Console.Read();        }

结果:

二:

static int TakesAWhile(int data, int ms)        {            Console.WriteLine("TakesAWhile started");            Thread.Sleep(ms);            Console.WriteLine("TakesAWhile completed");            return ++data;        }        public delegate int TakesAWhileDelegate(int data, int ms);        static void Main(string[] args)        {            TakesAWhileDelegate d1 = TakesAWhile;            // wait handle            IAsyncResult ar = d1.BeginInvoke(1, 3000, null, null);            while (true)            {                Console.Write(".");                if (ar.AsyncWaitHandle.WaitOne(500, false))                {                    Console.WriteLine("Can get the result now");                    break;                }            }            int result = d1.EndInvoke(ar);            Console.WriteLine("result: {0}", result);            Console.Read();        }

结果:

三:

  static int TakesAWhile(int data, int ms)         {
            Console.WriteLine("TakesAWhile started");             Thread.Sleep(ms);             Console.WriteLine("TakesAWhile completed");             return ++data;         }         public delegate int TakesAWhileDelegate(int data, int ms);         static void Main(string[] args)         {
            TakesAWhileDelegate d1 = TakesAWhile;             // async callback             d1.BeginInvoke(1, 3000, TakesAWhileCompleted, d1);             for (int i = 0; i < 100; i++)             {
                Console.Write(".");                 Thread.Sleep(500);             }             Console.Read();         }         static void TakesAWhileCompleted(IAsyncResult ar)         {
            if (ar == null) throw new ArgumentNullException("ar");             TakesAWhileDelegate d1 = ar.AsyncState as TakesAWhileDelegate;             Trace.Assert(d1 != null, "Invalid object type");             int result = d1.EndInvoke(ar);             Console.WriteLine("result: {0}", result);         }

结果:

四:

static int TakesAWhile(int data, int ms)        {            Console.WriteLine("TakesAWhile started");            Thread.Sleep(ms);            Console.WriteLine("TakesAWhile completed");            return ++data;        }        public delegate int TakesAWhileDelegate(int data, int ms);        static void Main(string[] args)        {            TakesAWhileDelegate d1 = TakesAWhile;            d1.BeginInvoke(1, 3000,            ar =>            {                int result = d1.EndInvoke(ar);                Console.WriteLine("result: {0}", result);            },            null);            for (int i = 0; i < 100; i++)            {                Console.Write(".");                Thread.Sleep(500);            }            Console.Read();        }

结果:同三

转载于:https://www.cnblogs.com/ChineseMoonGod/p/5354170.html

你可能感兴趣的文章
不规则物体形状匹配综述
查看>>
自动化设计-框架介绍 TestCase
查看>>
CJ看showgirl已经out!VR体验才是王道
查看>>
Manually Summarizing EIGRP Routes
查看>>
spring boot 1.5.4 整合webService(十五)
查看>>
modsecurity(尚不完善)
查看>>
获取.propertys文件获取文件内容
查看>>
Redis3.0.5配置文件详解
查看>>
Know about Oracle RAC Heartbeat
查看>>
JQuery——实现Ajax应用
查看>>
前端05.js入门之BOM对象与DOM对象。
查看>>
oracle kill所有plsql developer进程
查看>>
keepalived双机热备原理及实例部署LVS+keepalived
查看>>
曲线学习PyQt5方案一
查看>>
OpenCV学习】矩阵运算和操作2
查看>>
nginx+ffmpeg搭建rtmp转播rtsp流的flash服务器
查看>>
关于在arm裸板编程时使用printf问题的解决方法
查看>>
2015 上半年 JavaScript 使用统计数据
查看>>
《Python算法教程》——1.6 如果您感兴趣
查看>>
深度解析Java8 – AbstractQueuedSynchronizer的实现分析(下)
查看>>