C:\Users\Administrator\source\repos\weiCmd\20211013检测线程结束\

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _20211013检测线程结束
{
    class Program
    {
        static void Main(string[] args)
        {
            //辅助观察程序,每秒打印一个点
            Action printdian = print1s1dian;
            printdian.BeginInvoke(null,null);

            //定义一个尾托,用做线程程序
            Func<string, int, int> a = func01;
            int j;

            //检测线程结束方法一:while死循环
            IAsyncResult ar1 = a.BeginInvoke("线程开启", 0, null, null);
            while (ar1.IsCompleted == false)
            {
                Thread.Sleep(10);
            }
            j = a.EndInvoke(ar1);
            Console.WriteLine("死循环结束,线程执行结果:" + j);

            //检测线程结束方法二:等待句柄
            IAsyncResult ar2 = a.BeginInvoke("线程开启", 0, null, null);
            bool isEnd = ar2.AsyncWaitHandle.WaitOne(10 * 1000); //等待超时时间10秒
            if (isEnd)
            {
                j = a.EndInvoke(ar2);
                Console.WriteLine("等待句柄返回true,线程执行结果:" + j);
            }

            //检测线程结束方法三:回调函数
            IAsyncResult ar3 = a.BeginInvoke("线程开启", 0, huidiao, a); //倒是2参数是回调函数,倒数1参数是给回调函数传递的对象,回调函数的格式是固定的。


            //检测线程结束方法四:回调函数lambda表达式
            Thread.Sleep(5000);
            IAsyncResult ar4 = a.BeginInvoke("线程开启", 0, ar =>
            {
                j = a.EndInvoke(ar);
                Console.WriteLine("执行回调函数lambda表达式执行,线程执行结果:" + j);
            }, null);

            //1秒内能累加到多少?
            Int64 n = 0;
            DateTime dt1 = DateTime.Now;
            while ((DateTime.Now-dt1).TotalSeconds < 1 )
            {
                n++;
            }
            Console.WriteLine("1秒内累加到:"+n);

            Console.ReadKey();
        }

        /// <summary>
        /// 线程程序 应用程序线程
        /// </summary>
        /// <param name="s"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        static int func01(string s, int i)
        {
            Thread.Sleep(5000); //暂停5秒
            return i * 100;
        }

        /// <summary>
        /// 回调函数
        /// </summary>
        /// <param name="ar"></param>
        static void huidiao(IAsyncResult ar)
        {
            Func<String, int, int> a = ar.AsyncState as Func<String, int, int>;
            int j = a.EndInvoke(ar);
            Console.WriteLine("执行回调函数,线程执行结果:" + j);
        }

        /// <summary>
        /// 每秒打印一个点
        /// </summary>
        static void print1s1dian()
        {
            DateTime dt1 = DateTime.Now; //30秒内一直打印点
            while ((DateTime.Now - dt1).TotalMilliseconds < 30000) 
            {
                Console.Write(".");
                Thread.Sleep(1000);
            }
            Console.WriteLine("END");
        }
    }
}


/* 执行结果:
.....死循环结束,线程执行结果:0
.....等待句柄返回true,线程执行结果:0
.....执行回调函数,线程执行结果:0
.1秒内累加到:3454028
....执行回调函数lambda表达式执行,线程执行结果:0
..........END
 */


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值