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:
Then,
btn_ok()
might look like: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 withasync
andawait
. 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