• C#实现异步编程的两个简单机制(异步委托&定时器)及Thread实现多线程


    创建线程的常用方法:异步委托、定时器、Thread类

    理解程序、进程、线程三者之间的区别:简而言之,一个程序至少有一个进程,一个进程至少有一个线程
    进程就是在内存中运行的程序(即运行着的程序);一个程序一般只有一个进程,一个进程可以包含多个线程(多线程编程);

    委托本质是类,查看生产的微软中间代码即可知道,该类中包含 BeginInvoke和EndInvoke方法

    .class public auto ansi sealed Core.MyFirstDelege
        extends [mscorlib]System.MulticastDelegate
    {
        // Methods
        .method public hidebysig specialname rtspecialname 
            instance void .ctor (
                object 'object',
                native int 'method'
            ) runtime managed 
        {
        } // end of method MyFirstDelege::.ctor
    
        .method public hidebysig newslot virtual 
            instance object Invoke (
                object o
            ) runtime managed 
        {
        } // end of method MyFirstDelege::Invoke
    
        .method public hidebysig newslot virtual 
            instance class [mscorlib]System.IAsyncResult BeginInvoke (
                object o,
                class [mscorlib]System.AsyncCallback callback,
                object 'object'
            ) runtime managed 
        {
        } // end of method MyFirstDelege::BeginInvoke
    
        .method public hidebysig newslot virtual 
            instance object EndInvoke (
                class [mscorlib]System.IAsyncResult result
            ) runtime managed 
        {
        } // end of method MyFirstDelege::EndInvoke
    
    } // end of class Core.MyFirstDelege


    使用异步编程的简单机制一:异步委托
        委托类型的BeginInvoke和EndInvoke方法。
            BeginInvoke方法:
                参数组成:引用方法的参数列表+callback参数+state参数
                返回值:IAsyncResult接口类型(接口介绍:这个接口有两个重要属性:IsCompleted属性 bool类型 表示异步线程(方法)是否执行完成;
                AsyncResult类实现了IAsyncResult,它有一个重要的成员:AsyncDeleget 返回被调用的委托的引用)

          IAsyncResult类型的方法:.AsyncWaitHandle.WaitOne(50,false)   指定时间间隔内异步方法还没运行完毕 返回false  否则返回true
                
                方法被调用时做了哪些事:
                1.在线程池中获取一个线程,当这个线程被调度时,执行委托列表方法;
                2.返回给主线程一个IAsyncResult对象的引用;
                
            EndInvoke方法:
                参数组成:IAsyncResult类型
                返回值:被调用的委托的返回值类型
                
                 方法被调用时做了哪些事:
                 根据参数:IAsyncResult对象找到关联的线程,
                 如果这个线程已经执行完毕,EedInvoke做如下有些事:清理线程的状态和资源;找到引用方法的返回值,并把它作为自己的返回值返回。
                 如果这个线程没有执行完毕,调用线程就会停止并等待,知道清理完成并返回值。
                

    异步编程的三者模式:等待直到完成模式、轮询模式、回调模式.
    下面给个回调模式的列子:
        public class TestThreading
        {
            public static int Method(int n, int m)
            {
                Console.WriteLine("异步方法内部开始");
                System.Threading.Thread.Sleep(5000);
                return n > m ? n : m;
            }

            public static void CallBack(IAsyncResult ia)
            {

                AsyncResult ar = (AsyncResult)ia;
                MyDel del = (MyDel)ar.AsyncDelegate;
                int result = del.EndInvoke(ia);
                Console.WriteLine(result);

            }
        }
        
        static void Main(string[] args)
        {
                #region 异步编程与线程
                
                Console.WriteLine("Main中的处理代码。。。。。");
                MyDel del = new MyDel(TestThreading.Method);
                Console.WriteLine("BeginInvoke 方法调用之前");
                IAsyncResult ia = del.BeginInvoke(32, 100, TestThreading.CallBack, null);
                Console.WriteLine("BeginInvoke调用之后,Main继续做别的事情");
                System.Threading.Thread.Sleep(5000);
                Console.WriteLine("Main 程序结束");
                
                #endregion
        }
        
    使用异步编程的简单机制二:计时器
    .Net BCL 提供了好几个Timer类,在这里我只介绍System.Threading中的Timer
    首先看下Timer最常用的构造函数:
    Timer(TimerCallback callback,objec state,int dueTime,int period)

    下面分别介绍下各个参数,介绍完了你就会用Timer实现异步编程了:
    TimerCallback 是一个委托类型,定义如下   void TimerCallback(object state) 这个就是回调函数,计时器在每次时间到时执行回调方法
    state 就是要传给回调方法的参数
    dueTime 就是回调方法在第一次被执行之前的时间
    period 是两次调用回调方法之间的时间间隔

    Demo如下:

        public class TestTimer
        {
            public static void TimerCallBack(object o)
            {
                Console.WriteLine("{0},{1}",o,DateTime.Now.ToString("yy:MM:dd HH:mm:ss"));
            }
        }
        
         static void Main(string[] args)
        {
        
            #region Timer类的使用(System.Threading下的)使用Timer发异步(回调函数)

            Timer time = new Timer(TestTimer.TimerCallBack, "lxf", 5000, 1000);

            #endregion
        }

  • 相关阅读:
    [ubuntu]Grafana+Prometheus监控
    Sysbench(Bulk insert)项目实践详解
    【今日分享】什么人能带领团队赢?
    [unbuntu]tcpdump抓包规则命令
    Sysbench介绍概括总结
    Sysbench(Bulk insert)测试报告
    git error: does not have a commit checked out
    报错:Flink Could not find a file system implementation for scheme 'hdfs'
    报错:Flink Hadoop is not in the classpath/dependencies
    mac 安装免费的navicate
  • 原文地址:https://www.cnblogs.com/lxf1117/p/3847823.html
Copyright © 2020-2023  润新知