簡體   English   中英

Winform,如果我啟動新線程來執行一些耗時的任務,是否需要手動“關閉”線程?

[英]Winform, If I start a new thread to do some time consuming task, do I need to 'close' the thread manually?

初始化表單時,我在那里聲明了一個timer (該timerperiod值為1分鍾)

public Form3()
{
    InitializeComponent();
    timer_1m = new System.Threading.Timer(new TimerCallback(timerElapsed_test), null, 0, _1min);
}

每隔60秒,我就會啟動一個新線程來執行某些操作。

void timerElapsed_test(object sender) //timer period: 60 seconds
{
    Thread thread = new Thread(new ParameterizedThreadStart(Do));
    thread.Start(null);

}

void Do(object o)
{
    label1.Text = "triggerd at " + DateTime.Now.ToString("HH:mm:ss"); //assume this is a task needs to take a lot of time
    //Do I need to close this thread here? and how?
}

我需要手動close線程嗎? 如果是,怎么辦?

就像Idle_Mind所說的,一旦線程“退出”就可以了。

線程的例程完成后,垃圾回收將對其進行處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM