zoukankan      html  css  js  c++  java
  • 也写个异步调用 遥远的青苹果

     
     
    今天第一次用live Writer写代码,先发一个! 异步代码。
     
     
     
     
     
     
     
     
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
     
     
    namespace Asyncalback
    {
     
         
        public   delegate string deleDoing(string argsInput);
     
        class AsyncalCall
        {
     
      private static  deleDoing deleDoingObj = new deleDoing(SaySomeThingsNeedLitterOfTime); // 三部曲,定义委托,二 定义方法执行(主要是begininvoke,和endinvoke),和,三是返回结果存放位置
          private static string SomeThingsNeedLitterOfTimeResult = string.Empty; //返回结果的变量
      public static void Main()
            {
                 Console.WriteLine("开始工作。。。");
                 string inWorkinArges = "参数"; //费事的工作传入参数
     
                deleDoingObj.BeginInvoke(inWorkinArges, new AsyncCallback(TaskFinished), null); //执行
     
                doAnotherThinges();
     
                Console.ReadLine();
     
     
     
            }
     
     
     
     
            /// <summary>
            /// 模拟其它执行过程
            /// </summary>
            private static  void   doAnotherThinges()
            {
     
     
                for (int i = 0; i < 10; i++)
                {
     
                    Console.WriteLine(i);
     
                    Console.WriteLine("其它程序"+System.DateTime.Now.ToString());
                
     
                    Thread.Sleep(100);
                    Console.WriteLine("其它程序"+System.DateTime.Now.ToString());
                }
     
     
            }
     
     
     
            /// <summary>
            /// 返回的结果费时的结果 执行的情况
            /// </summary>
            /// <param name="result"></param>
            public static void TaskFinished(IAsyncResult result)
            {
                Console.WriteLine("执行结果回调一次"+System.DateTime.Now.ToString());
                 SomeThingsNeedLitterOfTimeResult = deleDoingObj.EndInvoke(result);
                 Console.WriteLine("执行结果回结束"+System.DateTime.Now.ToString());
                 Console.WriteLine("执行结果是:SomeThingsNeedLitterOfTimeResult" + SomeThingsNeedLitterOfTimeResult);
     
            }
     
         
     
     
            private static string SaySomeThingsNeedLitterOfTime(string argsInput)
            {
     
                string reture = string.Empty;
                int cout=0;
                for (int i = 0; i < 10; i++)
                {
     
                   
     
                    Console.WriteLine("执行费时程序"+System.DateTime.Now.ToString());
                    Thread.Sleep(100);
                    Console.WriteLine("执行费时程序"+System.DateTime.Now.ToString());
                    cout+=i;
     
                }
     
                reture = cout.ToString();
                return reture;
            
            
            }
        }
    }
     
    
  • 相关阅读:
    可视化数据挖掘开源软件的比较分析
    大数据平台比较-CDH,HDP
    数据挖掘的一般过程
    httpclient介绍与请求方式详解
    30分钟带你了解阻塞队列所有内容,再也不怕面试官刁难你了!(上)
    Lock
    HashMap 源码解读
    类加载各阶段详解
    Java基础复习(八、注解)
    Java基础复习(六、反射)
  • 原文地址:https://www.cnblogs.com/lixinhai/p/2531359.html
Copyright © 2011-2022 走看看