惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
Schneier on Security
Schneier on Security
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
A
About on SuperTechFans
Recorded Future
Recorded Future
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
V2EX - 技术
V2EX - 技术
N
Netflix TechBlog - Medium
F
Fortinet All Blogs
V
Visual Studio Blog
Martin Fowler
Martin Fowler
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Exploit Database - CXSecurity.com
F
Full Disclosure
Scott Helme
Scott Helme
H
Heimdal Security Blog
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
Application and Cybersecurity Blog
Application and Cybersecurity Blog
V
Vulnerabilities – Threatpost
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
WordPress大学
WordPress大学
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Troy Hunt's Blog
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
Jina AI
Jina AI
S
Securelist
小众软件
小众软件
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
AWS News Blog
AWS News Blog
N
News and Events Feed by Topic
博客园 - 三生石上(FineUI控件)
量子位

博客园 - Du大调

mysql 8 忘记密码 发生系统错误 1067 一些有特色的国外电子商务网站 margin在IE6下的问题。 4月4号参加北京俱乐部活动的收获 转化率极高的十个网站 使用aspnet_regsql.exe 创建ASPState数据库,用来保存session会话 (转)获取网购订单的12个技巧 每日词汇 每日英语 词汇 不是可以识别的 内置函数名称 电子商务网站用户体验--根据用户的习惯进行推荐 异常详细信息: System.Data.SqlClient.SqlException:过程或函数 需要参数 但未提供该参数。 不是有效的虚拟路径,未能映射路径 在IIS中设置Gzip页面压缩 刚做项目的时候 silverlight 不可 silverlight error message,ErrorCode:2254 ErrorCode: 1001 响应在此上下文中不可用
利用委托异步发送邮件
Du大调 · 2009-01-04 · via 博客园 - Du大调

故事是这样发生的:

越来越多的用户在网站购买了东西,每成功订下一个定单,就要向用户的邮箱发送一封EMAIL。当邮件正在发送时,占用的资源会非常多,用户也需要等待,用户越多,这样用户页面上的等待时间就会变得很长,为了解决这个问题,决定把发送EMAIL和短信的都改成异步执行的。

异步发送邮件,有两种做法;一种是大家在网上一搜就能找到一堆的发送方法,这种方法都是在发送email的时候再进行异步操作,如这篇文章(http://blog.csdn.net/liuduoqing/archive/2007/12/19/1952934.aspx)中写到的。

但是这种方法在我这里需要的成本太高,因为发送邮件的方法是封装好的,不便更改,所以只能从调用这个方法上想办法,在这里我找到了委托,可以异步调用方法,这样就可以实现了,委托的介绍在这里:http://msdn.microsoft.com/zh-cn/library/ms173171(VS.80).aspx

闲话少叙,把示例代码先贴出来:

Code

再说一下委托的方法,委托在类内类外都可以定义,声明了委托以后,就可以使用了,把要异步执行的方法通过下面的方式给一个委托变量,

Code

这样,就可以调用委托中提供的异步方法BeginInvoke来执行异步发送邮件.BeginInvoke的参数中AsyncResult callback是异步执行完成后调用的方法,

在调用的方法中一般要执行一下EndInvoke(IAsyncResult iar),一定要执行这个,不然的话据说后果很严重.

参数Object o是可以传入DelegateName的一个实例,指的是在当前线程上执行,也可传入null

对委托理解不是很深,不妥之处万望指教,谢谢

本博客内容公供学习研究用,和工作内容无关.