zoukankan      html  css  js  c++  java
  • 异步

    using System;
    //using System.Collections.Generic;
    //using System.Linq;
    //using System.Text;
    using System.Threading;
    using System.Net;
    
    namespace AsyBeginEndNoEncapsulationSimply
    {
        class Program
        {
            static void Main(string[] args)
            {
                ShowUriContent("http://www.cnblogs.com/DebugLZQ");//原同步方法
                ShowUriContentAsync("http://www.cnblogs.com/DebugLZQ");  //进行异步封装
                ShowUriContentAsync1("http://www.cnblogs.com/DebugLZQ");//简化1:Action简化
                ShowUriContentAsync2("http://www.cnblogs.com/DebugLZQ");//简化2:匿名方法简化
                ShowUriContentAsync3("http://www.cnblogs.com/DebugLZQ");//简化3:Lambda简化
                
    
                Thread.Sleep(50000);
            }
            //------进行异步封装
            public delegate void ShowUriContentDelegate(string text);
            static void ShowUriContentAsync(string uri)
            {
                ShowUriContentDelegate showUriContentDelegate = ShowUriContent;
                showUriContentDelegate.BeginInvoke(uri, ShowUriContentCompleted, showUriContentDelegate);
            }
    
            static void ShowUriContentCompleted(IAsyncResult result)
            {
                (result.AsyncState as ShowUriContentDelegate).EndInvoke(result);
            }
            //------进行异步封装--简化1:Action简化
            static void ShowUriContentAsync1(string uri)
            {
                Action<string> showUriContentDelegate = ShowUriContent;
                showUriContentDelegate.BeginInvoke(uri, ShowUriContentCompleted1, showUriContentDelegate);
            }
    
            static void ShowUriContentCompleted1(IAsyncResult result)
            {
                (result.AsyncState as Action<string>).EndInvoke(result);
            }
            //------简化2:匿名方法简化
            static void ShowUriContentAsync2(string uri)
            {
                Action<string> showUriContentDelegate = delegate(string uri_)
                {
                    using (WebClient client = new WebClient())
                    {
                        string text = client.DownloadString(uri_);
                        Display(text);
                    }
                };
                showUriContentDelegate.BeginInvoke(uri, delegate(IAsyncResult result) { (result.AsyncState as Action<string>).EndInvoke(result); }, showUriContentDelegate);
            }
            //------简化3:Lambda简化
            static void ShowUriContentAsync3(string uri)
            {
                Action<string> showUriContentDelegate = ( uri_)=>
                {
                    using (WebClient client = new WebClient())
                    {
                        string text = client.DownloadString(uri_);
                        Display(text);
                    }
                };
                showUriContentDelegate.BeginInvoke(uri, (result) => { (result.AsyncState as Action<string>).EndInvoke(result); }, showUriContentDelegate);
            }       
           
            //---------------------原同步方法
            static void ShowUriContent(string uri)
            {
                using (WebClient client = new WebClient())
                {
                    string text = client.DownloadString(uri);
                    Display(text);
                }
            }
    
            static void Display(string text)
            {
                Console.WriteLine(text.Length);
            }
        }
    }
  • 相关阅读:
    GUI 监听事件 (两个按钮,实现同一个监听)
    GUI 监听事件
    GUI 练习
    GUI 之表格布局
    GUI 之边界布局
    GUI 之流布局
    [转帖]Linux 下解压 rar 文件
    Linux 启动、停止、重启jar包脚本
    关于linux下,ls vi等命令失效的解决方法(配置下环境变量出现问题)
    超好用的UnixLinux 命令技巧 大神为你详细解读
  • 原文地址:https://www.cnblogs.com/wangchuang/p/5737009.html
Copyright © 2011-2022 走看看