3
What difference between using SmtpClient.SendAsync()
and create threads and execute the SmtpClient.Send()
for messaging? There is a lot of difference in performance or resource consumption?
3
What difference between using SmtpClient.SendAsync()
and create threads and execute the SmtpClient.Send()
for messaging? There is a lot of difference in performance or resource consumption?
2
The two methods produce the same result. The difference is only in the way you do it. One of them blocks the thread where it is called. The technique used in such cases is to create a thread dedicated to its execution not blocking the normal execution line. By question I imagine you know how to do.
Roughly that’s the same thing as doing layout of HTML with <table>
. The mechanism was not made for this, but as it had nothing better it was used and gives result.
But if you have a better mechanism, why not use it? The asynchronous method was created to offer a more convenient, easy-to-use and less likely bugs. Make something spin on threads separated is not as easy as it seems. there is a lot that can go wrong.
An example is having to turn around to cancel this thread, another is having to take a huge turn to know when it ends and do what it needs at the close of the thread. If you have an error (exception) you may not treat properly because you are in another thread.
The SmtpClient.SendAsync()
has a well-developed base to run without blocking the thread current and you don’t have to worry about how it does it. It may even be that it creates threads, but he’ll do his best and you don’t have to worry.
It features a sophisticated system including a cancellation technique, which is something most programmers ignore even the importance of it.
The most suitable is to use with await
.
On the other hand, it is not possible to send messages asynchronously in parallel. If you need this you will have to do the parallel sending on your own SmtpClient.send()
, probably in conjunction with the TPL or another mechanism (even all at hand). Or you can use another API that has all this more ready. An example.
2
smtpClient.send()
: Sends the specified message to an SMTP server for delivery. It will start sending in the main/ui thread and block.
smtpClient.SendAsync()
: Sends the specified email message on an SMTP server for delivery. This method does not block the call thread and allows the caller to transfer object to the method that is invoked when the operation is terminated. You will pick up a thread from the . NET thread pool and run the method in that segment. Therefore, your main UI will not lock or lock.
1
It’s simple.
If you use the .SendAsync()
he creates a new thread to send and to Main thread continues to read the lines as if nothing had happened. Then, when the new thread finish the method .SendAsync
it returns the answer in a callback, which must be configured.
The .Send
makes the Main thread wait for the answer of that method and that thread is stuck waiting for the .Send
break up.
If you use .SendAsync
you can send the message in parallel while doing something else, increasing the performance. But you have to be careful, don’t use threads so if you need the answer already in the main code stream because you might happen to go through that part of code and you don’t have it yet.
Browser other questions tagged c# .net thread emailing async
You are not signed in. Login or sign up in order to post.
but in the case I described above, using Smtpclient.Send() in Threads theoretically would not prevent Thread main from crashing?
– TotallyUncool
@Totallyuncool, Smtpclient.Send() sends the specified email message to an SMTP server for delivery. This method does not block the call segment and allows the "caller" to pass an object to the method that is invoked when the operation is completed.
– Taisbevalle
Help yourself to understand: http://www.macoratti.net/10/c_email2.htm and http://www.andrealveslima.com.br/blog/index.php/2015/05/13/enviando-e-mails-com-c/
– Taisbevalle