我有下面的代碼(但不工作)。我需要等待,直到線程完成其工作,然後再執行下一個命令我該如何等待線程完成其作業,然後執行下一個命令c#?
private void btnPaste_Click(object sender, EventArgs e)
{
if (copiedItems != null)
{
if (copy)
{
System.Threading.Thread thPASTE = new System.Threading.Thread(PasteFromCopy);
thPASTE.Start(currAddress);
lock (this)
{
btnRefresh_Click(sender, e);
}
}
else
{
System.Threading.Thread thPASTE = new System.Threading.Thread(PasteFromMove);
thPASTE.Start(currAddress);
lock (this)
{
btnRefresh_Click(sender, e);
}
}
}
}
粘貼應該做一些代碼,然後後,我應該刷新列表即時消息顯示..我需要這樣做,因爲它不適用於我當我調用線程刷新
我該怎麼做?
您可能還需要考慮一個私人鎖定變量,而不是採取鎖'this'。例如'私人只讀對象lockObject =新對象();'你可以看到這個線程的更多細節http://stackoverflow.com/questions/251391/why-is-lockthis-bad – 2012-02-07 22:36:33
有沒有必要開始一個線程和然後等待它。你也可以直接調用PasteFromCopy() – 2012-02-08 01:31:16