Search code examples
c#winapiasynchronousuwpwinrt-async

AsyncStatus, wait for Status completed or canceled


I have a function to send an e-mail:

public async Task<bool>  ComposeEmail(string email, string subject)
{
  var emailMessage = new Windows.ApplicationModel.Email.EmailMessage();
  emailMessage.Body = String.Empty;

  var emailRecipient = new Email.EmailRecipient(email);
  emailMessage.To.Add(emailRecipient);
  emailMessage.Subject = subject;

  return EmailManager.ShowComposeNewEmailAsync(emailMessage).status == AsyncStatus.Completed;
}

According to the docs, Started is not the final result.
How can I wait for the result?
I tried using await but then ShowComposeNewEmailAsync is returning void, and I can't get the Status.

I am following this link here: which is using async/await but with this method there is no way to get the result


Solution

  • Here is what I ended up doing eventually.

    public async Task<bool>  ComposeEmail(string email, string subject)
    {
        var emailMessage = new Windows.ApplicationModel.Email.EmailMessage();
        emailMessage.Body = String.Empty;
    
        var emailRecipient = new  Email.EmailRecipient(email);
        emailMessage.To.Add(emailRecipient);
        emailMessage.Subject = subject;
    
        var x = (EmailManager.ShowComposeNewEmailAsync(emailMessage));
    
        await x; 
        return x.Status == AsyncStatus.Completed;
    }
    

    However, the result here is not whether the User has actually sent the email or not. it seems to be if the program was lunched correctly or not.

    I am getting result completed even before sending or cancelling the Email.