zoukankan      html  css  js  c++  java
  • 事件异步(EAP)使用事件异步处理一些耗时操作

    比如需要下载一些比较大的文件,如果使用会UI卡顿,使用异步可以节省一些时间

    下面是一些例子:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    namespace Demo
    {
        class Program
        {
    
            static void Main(string[] args)
            {
                System.Net.WebClient client = new System.Net.WebClient();
                client.DownloadStringCompleted += new System.Net.DownloadStringCompletedEventHandler(client_DownloadStringCompleted);  //在异步资源下载操作完成时发生。
                client.DownloadStringAsync(new Uri("https://www.baidu.com"));  //异步下载地址
    
                for (int i = 0; i < 10;i++ )
                {
                    Console.WriteLine("异步操作是执行此代码:"+i);
                }
    
                Console.ReadKey();
            }
    
            public static void client_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
            {
                System.Net.WebClient client = sender as System.Net.WebClient;
                Console.WriteLine(e.Result);  //异步处理的结果
            }
    
        }
    }

    上述代码运行的结果:

  • 相关阅读:
    对java中接口的简单理解
    jqgrid
    sed跨行匹配替换
    linux 安装 mysql
    mysql 导入或导出(mysqldump)数据
    spring boot slf4j + logback
    原码、反码、补码
    Java线程池(一)
    springboot 多环境配置及打包资源
    springboot自定义yaml配置文件
  • 原文地址:https://www.cnblogs.com/linJie1930906722/p/5662059.html
Copyright © 2011-2022 走看看