如何异步在asp.net中两份工作 [英] How to Asynchronous two job in asp.net
问题描述
网络。我需要你们的支持,请帮助我。看到我的code ..
如果我的第一个作业完成,然后从 btn_ok code后面的出口和更新ASP.NET屏,但在同一时间工作2必须工作(群发电子邮件正在处理)
保护无效btn_ok(对象发件人,EventArgs的发送)
{尝试 { // **职位1:** CommonCls COM =新CommonCls();
com.SaveRecord(**参数值**); //注意:之后保存此,它必须从这个函数和更新信息给Web应用程序屏幕退出 // **工作2 **
EmailDAL EM =新EmailDAL();
.....
..... 尝试{
em.SendEmail(PTEmail,约会改期,亲爱的+ PTName +< BR>&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;约见+ PNAME + + GPName被称为 +已重新安排+表准时+< BR>在问候< BR>< b>中+ GPName +< / b>中+ axolbl);
}
赶上(例外前){logger.Error(ex.ToString()); }
尝试
{
em.SendEmail(PEmail,约会改期,亲爱的+ PNAME +< BR>&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;预约+ PTName +(+ PT $ + GPName +已重新安排+表准时+的简称)C $ C +。< BR>在问候< BR>< b>中+ GPName +< / b>中+ axolbl) ;
}
赶上(例外前){logger.Error(ex.ToString()); }
尝试
{
em.SendEmail(GPEmail,约会改期,亲爱的+ GPName +< BR>&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;预约+ PTName +(+ PT code +)以+ PNAME +已重新安排+表准时+< BR>在问候< BR>中+ axolbl);
}
赶上(例外前){logger.Error(ex.ToString()); }
}
赶上(异常前)
{}
} 赶上(异常前)
{}
}
电子邮件数据访问层
公共类EmailDAL
{ 内部字符串SMTP = ConfigurationManager.AppSettings [SMTPSERVER];
内部字符串MailAddress = ConfigurationManager.AppSettings [smtpUser];
内部字符串PWD = ConfigurationManager.AppSettings [smtpPass];
内部INT端口= Convert.ToInt16(ConfigurationManager.AppSettings [SMTPPORT]);
内部布尔SSL = Convert.ToBoolean(ConfigurationManager.AppSettings [EnableSsl]); 公共字符串SendEmail(字符串toMail,串mailSubject,字符串消息)
{ SmtpClient SMTPSERVER =新SmtpClient(SMTP);
VAR邮件= MAILMESSAGE新();
mail.From =新的MailAddress(MailAddress);
mail.To.Add(toMail);
mail.Subject = mailSubject;
mail.IsBodyHtml = TRUE;
mail.Body =< p =风格'行高度:30PX;>中+信息+< / P>中;
SmtpServer.Port =端口;
SmtpServer.UseDefaultCredentials = FALSE;
SmtpServer.Credentials =新System.Net.NetworkCredential(MailAddress,将PWD);
SmtpServer.EnableSsl = SSL;
尝试
{
SmtpServer.Send(邮件);
返回发送成功地;
}
赶上(异常前)
{
返回ex.Message.ToString();
}
}
}
**编辑答案
看到你的方法后,它看起来像你将需要你异步那EmailDAL.SendEmail()方法。要做到这一点,你可以做类似以下内容:
公共异步任务<串GT; SendEmailAsync(字符串toMail,串mailSubject,字符串消息){ SmtpClient SMTPSERVER =新SmtpClient(SMTP);
VAR邮件= MAILMESSAGE新();
mail.From =新的MailAddress(MailAddress);
mail.To.Add(toMail);
mail.Subject = mailSubject;
mail.IsBodyHtml = TRUE;
mail.Body =< p =风格'行高度:30PX;>中+信息+< / P>中;
SmtpServer.Port =端口;
SmtpServer.UseDefaultCredentials = FALSE;
SmtpServer.Credentials =新System.Net.NetworkCredential(MailAddress,将PWD);
SmtpServer.EnableSsl = SSL;
尝试{
等待SmtpServer.SendAsync(邮件);
返回发送成功;
}赶上(例外前){
返回ex.Message;
}
}
然后, btn_ok()
看起来像:
保护异步无效btn_ok(对象发件人,EventArgs的发送){尝试{
// **职位1:**
CommonCls COM =新CommonCls();
com.SaveRecord(**参数值**); //注意:之后保存此,它必须从这个函数和更新信息给Web应用程序屏幕退出 // **工作2 **
EmailDAL EM =新EmailDAL();
.....
..... 尝试{
伺机em.SendEmailAsync(PTEmail,约会改期,亲爱的+ PTName +< BR>&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;约见+ PNAME +被称为+ GPName +已重新安排+表准时+< BR>在问候< BR>< b>中+ GPName +< / b>中+ axolbl);
}
赶上(例外前){logger.Error(ex.ToString()); }
尝试{
伺机em.SendEmailAsync(PEmail,约会改期,亲爱的+ PNAME +< BR>&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;预约+ PTName +(+ PT code +)的简称+ GPName +已重新安排+表准时+< BR>在问候< BR>< b>中+ GPName +< / b>中+ axolbl );
}
赶上(例外前){logger.Error(ex.ToString()); }
尝试{
伺机em.SendEmailAsync(GPEmail,约会改期,亲爱的+ GPName +< BR>&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;预约+ PTName +(+ PT code +)以+ PNAME +已重新安排+表准时+< BR>在问候< BR>中+ axolbl);
}
赶上(例外前){logger.Error(ex.ToString()); }
}
赶上(例外前){}
}
看看下面的链接阅读更多关于使用 SmtpClient.SendAsync
方法,以及如何当电子邮件已经完成发射接收更新。
https://msdn.microsoft.com /en-us/library/x5x13z6h(v=vs.110).aspx
现在我们仍然需要看到什么 CommonCls.SaveRecord()
看起来像做一个异步。
**原来的答案
您可能需要使用尝试 Task.WhenAny()
以及异步
和的await
。它会很快返回作为单个作业完成但随后还是会继续让第二个作业完成。虽然我不能告诉你正在使用的是哪个版本的.NET。
下面是关于它的MSDN文章。希望这可以帮助:
https://msdn.microsoft.com/en-us/library/jj155756.aspx
Net. I need your favour Please help me. See my Code.. If My First job is finished then exit from btn_ok code behind and update to ASP.NET screen, but at the same time Job 2 must working (Bulk Email is processing)
protected void btn_ok(object sender, EventArgs e)
{
try
{
//**Job 1:**
CommonCls com = new CommonCls();
com.SaveRecord(**Parameter Values**);
//Note :after save this, it must exit from this function and update Message to web Application Screen
//**Job 2**
EmailDAL em = new EmailDAL();
.....
.....
try {
em.SendEmail(PTEmail, "Appointment Rescheduled ", "Dear " + PTName + "<br> Appointment with " + PName + " referred by " + GPName + " has been rescheduled " + Stime + ". <br> with Regards <br> <b>" + GPName + "</b>" + axolbl);
}
catch (Exception ex) { logger.Error(ex.ToString()); }
try
{
em.SendEmail(PEmail, "Appointment Rescheduled ", "Dear " + PName + "<br> Appointment for " + PTName + "(" + PTCode + ") referred by " + GPName + " has been rescheduled " + Stime + ". <br> with Regards <br> <b>" + GPName + "</b>" + axolbl);
}
catch (Exception ex) { logger.Error(ex.ToString()); }
try
{
em.SendEmail(GPEmail, "Appointment Rescheduled ", "Dear " + GPName + "<br> Appointment for " + PTName + "(" + PTCode + ") with " + PName + " has been rescheduled " + Stime + ". <br> with Regards <br> " + axolbl);
}
catch (Exception ex) { logger.Error(ex.ToString()); }
}
catch (Exception ex)
{ }
}
catch (Exception ex)
{ }
}
Email Data Access Layer
public class EmailDAL
{
internal string SMTP = ConfigurationManager.AppSettings["smtpServer"];
internal string MailAddress = ConfigurationManager.AppSettings["smtpUser"];
internal string Pwd = ConfigurationManager.AppSettings["smtpPass"];
internal int Port = Convert.ToInt16(ConfigurationManager.AppSettings["smtpPort"]);
internal bool ssl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
public string SendEmail(string toMail, string mailSubject, string Message)
{
SmtpClient SmtpServer = new SmtpClient(SMTP);
var mail = new MailMessage();
mail.From = new MailAddress(MailAddress);
mail.To.Add(toMail);
mail.Subject = mailSubject;
mail.IsBodyHtml = true;
mail.Body = "<p style='line-height: 30px;'>" + Message + "</p>";
SmtpServer.Port = Port;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential(MailAddress, Pwd);
SmtpServer.EnableSsl = ssl;
try
{
SmtpServer.Send(mail);
return "Send Sucessfully";
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
}
**Edited Answer After seeing your methods, it looks like you will need to make that EmailDAL.SendEmail() method of yours async. To do that you could do something like the following:
public async Task<string> SendEmailAsync(string toMail, string mailSubject, string Message) {
SmtpClient SmtpServer = new SmtpClient(SMTP);
var mail = new MailMessage();
mail.From = new MailAddress(MailAddress);
mail.To.Add(toMail);
mail.Subject = mailSubject;
mail.IsBodyHtml = true;
mail.Body = "<p style='line-height: 30px;'>" + Message + "</p>";
SmtpServer.Port = Port;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential(MailAddress, Pwd);
SmtpServer.EnableSsl = ssl;
try {
await SmtpServer.SendAsync(mail);
return "Send Successfully";
} catch(Exception ex) {
return ex.Message;
}
}
Then, btn_ok()
might look like:
protected async void btn_ok(object sender, EventArgs e) {
try {
//**Job 1:**
CommonCls com = new CommonCls();
com.SaveRecord(**Parameter Values**);
//Note :after save this, it must exit from this function and update Message to web Application Screen
//**Job 2**
EmailDAL em = new EmailDAL();
.....
.....
try {
await em.SendEmailAsync(PTEmail, "Appointment Rescheduled ", "Dear " + PTName + "<br> Appointment with " + PName + " referred by " + GPName + " has been rescheduled " + Stime + ". <br> with Regards <br> <b>" + GPName + "</b>" + axolbl);
}
catch (Exception ex) { logger.Error(ex.ToString()); }
try {
await em.SendEmailAsync(PEmail, "Appointment Rescheduled ", "Dear " + PName + "<br> Appointment for " + PTName + "(" + PTCode + ") referred by " + GPName + " has been rescheduled " + Stime + ". <br> with Regards <br> <b>" + GPName + "</b>" + axolbl);
}
catch (Exception ex) { logger.Error(ex.ToString()); }
try {
await em.SendEmailAsync(GPEmail, "Appointment Rescheduled ", "Dear " + GPName + "<br> Appointment for " + PTName + "(" + PTCode + ") with " + PName + " has been rescheduled " + Stime + ". <br> with Regards <br> " + axolbl);
}
catch (Exception ex) { logger.Error(ex.ToString()); }
}
catch (Exception ex) { }
}
Check out the link below to read more about using the SmtpClient.SendAsync
method and how to receive updates when the email has finished transmitting.
https://msdn.microsoft.com/en-us/library/x5x13z6h(v=vs.110).aspx
Now we still need to see what CommonCls.SaveRecord()
looks like to make that one async.
**Original Answer
You may want to try using Task.WhenAny()
along with async
and await
. It will return as soon as a single job finishes but then it will still continue to let the second job finish. Although I cannot tell what version of .NET you are working with.
Below is an MSDN article about it. Hope this helps: https://msdn.microsoft.com/en-us/library/jj155756.aspx
这篇关于如何异步在asp.net中两份工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!