How to Asynchronous two job in asp.net

后端 未结 1 1147
执念已碎
执念已碎 2021-01-29 06:08

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 sam

1条回答
  •  难免孤独
    2021-01-29 06:24

    **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 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 = "

    " + Message + "

    "; 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 + "
         Appointment with " + PName + " referred by " + GPName + " has been rescheduled " + Stime + ".
    with Regards
    " + GPName + "" + axolbl); } catch (Exception ex) { logger.Error(ex.ToString()); } try { await em.SendEmailAsync(PEmail, "Appointment Rescheduled ", "Dear " + PName + "
         Appointment for " + PTName + "(" + PTCode + ") referred by " + GPName + " has been rescheduled " + Stime + ".
    with Regards
    " + GPName + "" + axolbl); } catch (Exception ex) { logger.Error(ex.ToString()); } try { await em.SendEmailAsync(GPEmail, "Appointment Rescheduled ", "Dear " + GPName + "
         Appointment for " + PTName + "(" + PTCode + ") with " + PName + " has been rescheduled " + Stime + ".
    with Regards
    " + 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

    0 讨论(0)
提交回复
热议问题