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);
            }
        }
    }
  • 相关阅读:
    [POJ2752]Seek the Name, Seek the Fame
    [HDOJ1247]Hat’s Words
    [POJ2001]Shortest Prefixes
    不用代码,10分钟打造属于自己的第一款小程序
    不用代码,10分钟打造属于自己的第一款小程序
    不用代码,10分钟打造属于自己的第一款小程序
    移动端iPhone系列适配问题的一些坑
    移动端iPhone系列适配问题的一些坑
    【前端统计图】echarts多条折线图和横柱状图实现
    【前端统计图】echarts多条折线图和横柱状图实现
  • 原文地址:https://www.cnblogs.com/wangchuang/p/5737009.html
Copyright © 2011-2022 走看看