zoukankan      html  css  js  c++  java
  • (转载)C# 异步委托的使用

    当进行耗时的工作时,可以使用异步委托执行。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Remoting.Messaging;

    namespace ConsoleApplication2
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                facHandler f 
    = new facHandler(Fac);
                
    //开异步方法开始执行
                IAsyncResult result = f.BeginInvoke(10new AsyncCallback(callBack), "计算结束");
                Console.WriteLine(
    "做其他事情先……");
                Console.Read();
            }


            
    delegate int facHandler(int n);
            
    static int Fac(int n)
            {
                
    if (n == 1return 1;
                System.Threading.Thread.Sleep(
    100);
                
    return Fac(n - 1* n;
            }

            
    static void callBack(IAsyncResult result)
            {
                
    //result是Fac()的返回值
                
    //将AsyncDelegate强制转换为用户定义的委托。
                facHandler handler = (facHandler)((AsyncResult)result).AsyncDelegate;
                Console.WriteLine(
    "结果:" + handler.EndInvoke(result));

                Console.WriteLine(result.AsyncState);
            }

        }
    }
  • 相关阅读:
    Radmin View3.5
    delphi安装fastreport6
    大华监控设备配置文件导入生成工具
    大华监控和天地伟业监控免输密码登录助手
    锐捷交换机常用配置命令【加精】
    锐捷交换机的配置命令大全
    H3C常用配置命令
    锐捷交换机配置命令
    Delphi 随手笔记,使用了DEV控件组件
    读取软件的版本信息 GetFileVersionInfo
  • 原文地址:https://www.cnblogs.com/arraylist/p/2132057.html
Copyright © 2011-2022 走看看