What is the difference between SmtpClient.SendAsync and SmtpClient.Send () using Thread?

2

What is the difference between using SmtpClient.SendAsync() and creating threads and running SmtpClient.Send() for sending messages? Is there a lot of difference in performance or resource consumption?

    
asked by anonymous 05.10.2016 / 15:07

3 answers

1

Both methods produce the same result. The difference is only in the way of doing. One of them blocks the thread that is called. The technique that is often used in these cases is to create a dedicated thread for its execution by not blocking the normal thread. By the question I imagine you know how to do it.

This is roughly the same thing as layout of HTML with <table> . The engine was not made for this, but since it had nothing better it was used and gives results.

But if you have a better mechanism, why not use it? The asynchronous method was designed to offer a more convenient, easy-to-use, and less buggy approach. Making something run in separate threads is not as easy as it sounds. There's a lot that can go wrong.

For example, 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 end of the thread >. If you have an (exception) error it may not handle properly because it is in another thread .

SmtpClient.SendAsync() has a well-developed foundation for running without blocking thread and you do not have to worry about how it does it. It may even be that it creates threads , but it will do the best and you do not have to worry.

It has a sophisticated system including a cancellation technique, which is something that most programmers ignore even the importance of it.

It is best to use 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 using SmtpClient.send() , probably in conjunction with TPL or another mechanism (until even everything at hand). Or you can use another API that has it all done. An example .

    
06.10.2016 / 03:25
1

smtpClient.send() : Sends the specified message to an SMTP server for delivery . It will start sending in thread main / ui and it would block.

smtpClient.SendAsync() : Sends the specified e-mail message to an SMTP server for delivery. This method does not block the calling thread and allows the caller to transfer object to the method that is invoked when the operation is terminated. It will take a thread from the .NET thread pool and execute the method on that thread. Therefore, your primary UI will not lock or block.

    
05.10.2016 / 15:15
1

It's simple.

If you use .SendAsync() it creates a new thread to do this send and the Main thread continues to read the lines as if nothing had happened. Then, when the new thread terminates the .SendAsync method it returns the response in a callback method, which must be configured.

The .Send causes the Main thread to wait for the response of this method and this thread gets caught waiting for the .Send to finish.

If you use .SendAsync you can send the message in parallel while doing something else, increasing performance . But you have to be careful, do not use threads so if you need the answer already in the main code flow because it might happen to go through that piece of code and you do not have it yet.     

06.10.2016 / 03:45