zoukankan      html  css  js  c++  java
  • 关于异步委托的调用与应用场景

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Threading;
    using System.Runtime.Remoting.Messaging;
    using System.IO;
    
    namespace TestApp.ResponseHeader
    {
        public partial class Demo : System.Web.UI.Page
        {
            public delegate void SendMailHandler();
            protected void Page_Load(object sender, EventArgs e)
            {
                Response.ClearHeaders();
                Response.AddHeader("Server", "nginx");
                Response.AddHeader("testID", Session.SessionID);
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                string userName = this.TextBox1.Text;
                Response.Write(string.Format("当前注册的用户是:{0}",userName));
    
                SendMailHandler sendMailHandler = new SendMailHandler(SendMail);
                AsyncCallback callBack = new AsyncCallback(SendMailCallBack);
                sendMailHandler.BeginInvoke(callBack, userName);
            }
    
    
            public void SendMailCallBack(IAsyncResult re)
            {
                AsyncResult result = re as AsyncResult;
                SendMailHandler handler = (SendMailHandler)result.AsyncDelegate;
                handler.EndInvoke(re);
    
                WriteLog(string.Format("{0},您的注册邮件已经发送...", re.AsyncState));
                WriteLog(string.Format("{0}::邮件发送完毕...", DateTime.Now));
            }
    
            private void SendMail()
            {
                WriteLog(string.Format("{0}::开始发送邮件...", DateTime.Now));
                Thread.Sleep(20000);
    
                //ThreadStart ts = new ThreadStart(Run);
    
                string s = "gogogo...";
                ParameterizedThreadStart pts = new ParameterizedThreadStart(Run);
                Thread t = new Thread(pts);
                t.Start(s);
            }
    
            private void WriteLog(string log)
            {
               StreamWriter sw = File.AppendText(Server.MapPath("~/text.txt"));
               sw.WriteLine(log);
               sw.Flush();
               sw.Close();
            }
    
    
            public void Run(object s)
            {
                WriteLog(s.ToString());
            }
        }
    }

    结果如下图

  • 相关阅读:
    test
    VS dll 引用依赖
    Git配置
    编码--文字输入的前因后果
    base64相关
    异或
    UNION / UNION ALL 区别
    数据库使用规范
    chrome插件开发学习(一)
    缓存穿透 缓存雪崩 缓存并发
  • 原文地址:https://www.cnblogs.com/huangzelin/p/2830382.html
Copyright © 2011-2022 走看看