我们是否需要处置或使用终止后,在C#中的线程? [英] Do we need to dispose or terminate a thread in C# after usage?
问题描述
我有以下代码:
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.
我需要以某种方式处理创建的线程的Dispose( mailThread )?
或当它完成它的工作它会自动处理?
Do I need to somehow handle the dispose of the created thread (mailThread)? Or does it automatically dispose when it finishes its job?
请不建议SendAsync方法。我想手动创建线程。 Mail.Send只是一个示例场景。
Please do not recommend the SendAsync method. I would like to create the thread manually. Mail.Send was only an example scenario.
感谢您。
推荐答案
NO!
有没有必要处置Thread对象(顺便说一句,Thread类没有提供Dispose方法)。
there is no need to dispose the Thread object (BTW, the Thread class does not provide the Dispose method).
这篇关于我们是否需要处置或使用终止后,在C#中的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!