How to Asynchronous two job in asp.net

可紊 提交于 2019-12-02 14:11:31

**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> &nbsp;&nbsp;&nbsp;&nbsp; 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> &nbsp;&nbsp;&nbsp;&nbsp; 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> &nbsp;&nbsp;&nbsp;&nbsp; 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!