[英]How to call a method asynchronously
我试过跟这个链接异步调用,但有些类已经过时了。
所以我想为我的项目准确回答。
public class RegisterInfo
{
public bool Register(UserInfo info)
{
try
{
using (mydatabase db = new mydatabase())
{
userinfotable uinfo = new userinfotable();
uinfo.Name = info.Name;
uinfo.Age = info.Age;
uinfo.Address = info.Address;
db.userinfotables.AddObject(uinfo);
db.SaveChanges();
// Should be called asynchronously
Utility.SendEmail(info); // this tooks 5 to 10 seconds or more.
return true;
}
}
catch { return false; }
}
}
public class UserInfo
{
public UserInfo() { }
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
}
public class Utility
{
public static bool SendEmail(UserInfo info)
{
MailMessage compose = SomeClassThatComposeMessage(info);
return SendEmail(compose);
}
private static bool SendEmail(MailMessage mail)
{
try
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.something.com";
client.Port = 123;
client.Credentials = new System.Net.NetworkCredential("username@domainserver.com", "password");
client.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
client.Send(mail);
return true;
}
catch { return false; }
}
}
请查看Register
方法。 保存数据后,我不想等待发送邮件。 如果可能的话,我想处理在其他线程上发送邮件,这样用户就不会等待更长的时间。
我不需要知道邮件是否已成功发送。
希望你能明白我的意思。 对不起,我的英语不好。
使用Thread
:
new Thread(() => Utility.SendEmail(info)).Start();
使用ThreadPool
:
ThreadPool.QueueUserWorkItem(s => Utility.SendEmail(info));
使用Task
:
Task.Factory.StartNew(() => Utility.SendEmail(info));
当然, Thread
和ThreadPool
需要using System.Threading
而Task
需要using System.Threading.Tasks
正如David Anderson所说,SmtpClient已经支持异步发送 (我可能应该注意函数的内容而不是回答问题) ,所以从技术上讲,你可以使用它来处理发送,尽管它不会卸载处理你的整个方法。
请尝试以下链接: ThreadPool.QueueUserWorkitem
SmtpClient已经有了SendAsync方法。 您不需要编写自己的异步代码来执行此操作。
关于SmtpClient的@Comment不能与ASP.NET一起开箱即用:
这绝对不是真的,它运行良好,是推荐的API。 但是,您必须了解ASP.NET Page Life-Cycl e以及线程在服务器上的行为方式。 否则没有理由不使用它。
或者使用Async CTP 。
public async Task<bool> Register(UserInfo info)
{
try
{
using (mydatabase db = new mydatabase())
{
userinfotable uinfo = new userinfotable();
uinfo.Name = info.Name;
uinfo.Age = info.Age;
uinfo.Address = info.Address;
db.userinfotables.AddObject(uinfo);
db.SaveChanges();
//Wait for task to finish asynchronously
await Utility.SendEmail(info);
return true;
}
}
catch { return false; }
}
private Task SendEmail(MailMessage mail)
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.something.com";
client.Port = 123;
client.Credentials = new System.Net.NetworkCredential("username@domainserver.com", "password");
client.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
//The smtp SendTaskAsync is an extension method when using Async CTP
return client.SendTaskAsync("from", "recipients", "subject", "body");
}
您的原始代码中也存在错误。 当SendEmail函数内部抛出异常时,它返回false,但在寄存器函数内它仍将返回true。 假设bool表示成功或失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.