zoukankan      html  css  js  c++  java
  • 1.初探

    1.线程需要通过委托去开启。

     private static void Main(string[] args)
            {
                //通过委托开启线程
                Func<int, string, int> a = Test;
                //开启新的线程去执行a所引用的函数
                //IAsyncResult 取得当前线程状态
                IAsyncResult ar = a.BeginInvoke(100, "maning", null, null);
    
                //可以认为线程是同时执行的(异步执行)
                Console.WriteLine("main");
                while(ar.IsCompleted == false)
                {
                    Console.Write(".");
                    Thread.Sleep(10);
                }
                int res = a.EndInvoke(ar);
                Console.WriteLine(res);

           bool isEnd = ar.AsyncWaitHandle.WaitOne(1000);//等待1000毫秒,1000毫秒结束,返回true,没结束返回false
                if(isEnd)
                {
                    int res = a.EndInvoke(ar);
                    Console.WriteLine(res);
                }
                Console.ReadKey(); Console.ReadKey(); }
    private static int Test(int i, string name) { Console.WriteLine("Test" + i + " " + name); //当前线程暂停100ms Thread.Sleep(100); return 100; }
    IAsyncResult ar = a.BeginInvoke(100, "maning", null, null);
    ar储存当前线程运行状态


    int res = a.EndInvoke(ar);
    res 取得当前线程运行结果。

    Thread.Sleep(100)当前线程暂停100ms
    ar.AsyncWaitHandle.WaitOne(1000)
    
    
    等待1000毫秒,1000毫秒结束,返回true,没结束返回false,只会等待1000ms。如果执行700ms,等待700ms,若超过1000ms,只会等待1000ms。
     
    
    
    
  • 相关阅读:
    JMS(面向消息中间件)
    ActiveMQ消息中间件知识汇总
    linux安装mysql常见命令
    结果集耗尽时,检查是否关闭结果集时常用sql
    Spring注解驱动开发之事务概念
    nginx 基础
    HTTP原理
    MYSQL----cmake 数据库出错
    php安装Phalcon模块
    docker报错 Failed to start Docker Application Container Engine.
  • 原文地址:https://www.cnblogs.com/Vincente/p/6912703.html
Copyright © 2011-2022 走看看