doesn't it need to delete thread object?

i have a search thread in my project . the thread is created in 'Form1()' function:

objSearchThread = new Thread(this.Thread_Func);

when user clicks the 'search' button, Start() function is called:

private void Button_Search_Click(object sender, EventArgs e)
{
    objSearchThread.Start();
}

second clicked of this button, crashed! because the thread is 'Started' state. if i change my button clicked code, and i add 'new' command. it works without error or crashing:

private void Button_Search_Click(object sender, EventArgs e)
{
    objSearchThread = new Thread(this.Thread_Func);
    objSearchThread.Start();
}

doesn't it need to delete thread object(objSearchThread )? does it need to call Abort() or other functions, when thread working ends? is second code that i write here correct?

Jon Skeet
people
quotationmark

No, you don't need to do anything. The thread will just finish when it has no more work to do. You might want to consider scheduling it to execute on the thread pool however, instead of creating a new thread each time. You could do that directly, or via the Task Parallel Libray (TPL) with the Task API. Alternatively, you might want to use BackgroundWorker, as that makes it easier to report progress to the UI. (Depending on what you're doing, you may not even need another thread at all - if you're calling a web service for example, you may be able to use the async facilities in C# 5 to make the code simpler and more efficient in one go...)

If you're using Thread directly, however, you do need to create a new Thread object each time you want to start it, as you can't reuse a thread.

Additionally, unless you need this to be an instance variable, you should consider making it a local variable. When do you ever access the thread other than in method?

people

See more on this question at Stackoverflow