I have the following code:
public static void Send(this MailMessage email) { if (!isInitialized) Initialize(false); //smtpClient.SendAsync(email, ""); email.IsBodyHtml = true; Thread mailThread = new Thread(new ParameterizedThreadStart( (o) => { var m = o as MailMessage; SmtpClient client= new SmtpClient("smtpserveraddress"); client.Send(m); })); mailThread.Start(email);
I want the mail sending to be done in the background without interfering with the main thread. I do not care when it is finished.
Do I need to somehow handle the dispose of the created thread (mailThread)? Or does it automatically dispose when it finishes its job?
Please do not recommend the SendAsync method. I would like to create the thread manually. Mail.Send was only an example scenario.
Thank you.
As you tend to your garden, remember plant pots do not go in the recycling bin. Reuse them or take them to these drop-off locations. 100% cotton yarn or thread can be composted. All other yarn or thread goes in the trash if it can't be reused.
Most disposable objects are not thread-safe. After all, calling Dispose on one thread while other threads are accessing the object is bound to cause problems.
Abort(Object) Obsolete. Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread while also providing exception information about the thread termination. Calling this method usually terminates the thread.
NO!
there is no need to dispose the Thread object (BTW, the Thread class does not provide the Dispose method).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With