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);
            }
        }
    }
  • 相关阅读:
    xlrd模块
    魔法路由ViewSetMixin
    AES加密
    orm的增删改查
    引入方式+样式+选择器
    视图+sql注入+事务+存储过程
    mysql用户管理+pymysql模块
    记录的详细操作
    约束
    一次http请求参数问题
  • 原文地址:https://www.cnblogs.com/wangchuang/p/5737009.html
Copyright © 2011-2022 走看看