0

I have read this question Is this a proper way of calling synchronous methods asynchronously? which is similar to my question, but not quite the same.

I have a method

Private Sub SendEmail(strFromAddress As String, strFromName As String, strSubject As String, strBody As String, strToAddress As String, strAttachment As String)

that is used for sending e-mail messages from within my program. However, to avoid a slight delay to the user, I have created an asynchronous method

Public Sub AsyncSendEmail(state As Object)

which basically calls SendEmail(). I am invoking AsyncSendEmail() like so:

ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf AsyncSendEmail), params)

I am wondering if this is a correct way of passing off a synchronous method call into an asynchronous method so that it doesn't hold up the UI.

Community
  • 1
  • 1
dub stylee
  • 3,252
  • 5
  • 38
  • 59
  • Working with UI imho [BackgroundWorker](https://msdn.microsoft.com/de-de/library/ywkkz4s1.aspx) is a solid solution – Alex B. Jan 27 '16 at 20:17
  • I see. I don't need the method to work with the UI necessarily, I just want to run it asynchronously so that it doesn't hold up the UI thread. – dub stylee Jan 27 '16 at 22:02
  • If you are using [`SmptClient`](https://msdn.microsoft.com/en-us/library/System.Net.Mail.SmtpClient%28v=vs.110%29.aspx) and .NET 4.5+, then using the [`SendMailAsync`](https://msdn.microsoft.com/en-us/library/hh193922%28v=vs.110%29.aspx) method with `Async`/`Await` would be another option. – Mark Jan 27 '16 at 23:23
  • @Mark thanks, unfortunately I am stuck targeting .NET 4 for the time being because there are still a handful of users running XP... it is good to know that I will have that to look forward to once we can target .NET 4.5+ though! – dub stylee Jan 27 '16 at 23:40
  • Have you just tried your code if it is working? Nevertheless I would always prefer [Task](https://msdn.microsoft.com/de-de/library/system.threading.tasks.task%28v=vs.100%29.aspx) over ThreadPool. – Alex B. Jan 28 '16 at 11:22
  • The code does work as expected, I was just wondering if this was the correct way to go about achieving this. I will look into using `Task` instead. – dub stylee Jan 28 '16 at 18:39

0 Answers0