• 异步


    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);
            }
        }
    }
  • 相关阅读:
    MongoDB安装&启动
    MongoDB集群搭建
    树与二叉树
    git入门
    MongoDB Java Driver
    Spring整合Junit4
    SQL字符串的数字部分递增
    [求职经历反面教材]4周面试20家,面霸磨成面瘫,仅供初级程序员参考!
    简陋的信息采集方式
    由一个博问学到的SQL查询方法 (一道多对多关系查询的面试题)
  • 原文地址:https://www.cnblogs.com/wangchuang/p/5737009.html
Copyright © 2020-2023  润新知