zoukankan      html  css  js  c++  java
  • net异步线程获取返回值的三种方式

    方式一:endInvoke

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    
    namespace ConsoleApplication6
    {
        //调用异步方法,并返回值:
        //方式一,采用轮训的方式;
        //申明一个委托;
        public delegate int del(int a,int b);
        class Program
        {
            private static int add(int i, int j)
            {
                Console.WriteLine("starting task....");
                Thread.Sleep(2000);
                int result = i + j;
                return result;
    
            }
            static void Main(string[] args)
            {
                del d = new del(add);
                IAsyncResult re = d.BeginInvoke(10,12,null,null);
                while (!re.IsCompleted)
                {
                    Console.WriteLine("异步线程进行中....");
                }
                int result = d.EndInvoke(re);
                Console.WriteLine("result:"+result);
                Console.ReadLine();
    
            }
        }
    }

    方式二:回调函数

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    
    namespace ConsoleApplication6
    {
        
       
        class Program
        {
            public delegate int del(int a,int b);
            static del d;
            static double result = 0; //使用全局的变量
    
            private static int add(int i, int j)
            {
                Console.WriteLine("starting task....");
                Thread.Sleep(2000);
                int result = i + j;
                return result;
    
            }
            private static void TaskFininshed(IAsyncResult ias)
            {
                result=d.EndInvoke(ias);
               
                Console.WriteLine("我来自异步线程...");
                Console.WriteLine("异步结束!输出结果");
                Console.WriteLine(result);
            }
            static void Main(string[] args)
            {
                 d= new del(add);
                IAsyncResult re = d.BeginInvoke(10,12,new AsyncCallback(TaskFininshed),null);
    
                Console.WriteLine("我来自主线程...."); 
                Console.WriteLine("这个时候,主线中的代码,不会被堵塞....");
    
                Console.ReadLine();//异步线程 是属于 后台线程,所以这里要阻止前台线程结束
            }
        }
    }

    waiteone的使用滴呀

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    
    namespace ConsoleApplication6
    {
        
       
        class Program
        {
            public delegate int del(int a,int b);
            static del d;
            static double result = 0; //使用全局的变量
    
            private static int add(int i, int j)
            {
                Console.WriteLine("starting task....");
                Thread.Sleep(2000);
                int result = i + j;
                return result;
    
            }
            private static void TaskFininshed(IAsyncResult ias)
            {
                result=d.EndInvoke(ias);
               
                Console.WriteLine("我来自异步线程...");
                Console.WriteLine("异步结束!输出结果");
                Console.WriteLine(result);
            }
            static void Main(string[] args)
            {
                 d= new del(add);
                IAsyncResult re = d.BeginInvoke(10,12,new AsyncCallback(TaskFininshed),null);
    
                Console.WriteLine("我来自主线程...."); 
                Console.WriteLine("这个时候,主线中的代码,不会被堵塞....");
    
                //Console.ReadLine();//异步线程 是属于 后台线程,所以这里要阻止前台线程结束
                //这里我们不一定要使用  Console.ReadLine() 来等待异步线程的结束;
    
                //我们还可以 使用:
                while (!re.AsyncWaitHandle.WaitOne(1000,false))
                {
                    Console.WriteLine("主线程,在指定的时间内等你回来呀,亲....");
                    //如果指定时间为0,表示不等待,如果为-1,表示永远等待
    
                }
    
                Console.WriteLine("同步 异步 都结束了....");
                Console.ReadLine();
    
                /*
                 * 
                 * 
                 *  WaitOne的第一个参数表示要等待的毫秒数,
                 * 在指定时间之内,WaitOne方法将一直等待,直到异步调用完成,
                 * 并发出通知,WaitOne方法才返回true。
                 * 当等待指定时间之后,异步调用仍未完成,WaitOne方法返回false,
                 * 如果指定时间为0,表示不等待,如果为-1,表示永远等待,直到异步调用完成。
                 * 
                 * */
            }
        }
    }

    再看当waitone的时间设置不一样时候的结果;

  • 相关阅读:
    Spring MVC 完全注解方式配置web项目
    spring WebServiceTemplate 调用 axis1.4 发布的webservice
    修改Intellij Idea 创建maven项目默认Java编译版本
    Git Commit提交规范和IDEA插件Git Commit Template的使用
    myEclipse10安装以及破解
    ES6中Map与其他数据结构的互相转换
    ES6用来判断数值的相关函数
    WebStorm使用码云插件问题
    Css解决表格超出部分用省略号显示
    Js/Jquery获取网页屏幕可见区域高度
  • 原文地址:https://www.cnblogs.com/mc67/p/5623698.html
Copyright © 2011-2022 走看看