簡體   English   中英

我如何等待線程完成其工作,然后執行下一個命令C#?

[英]how do i wait till the thread complete its job then execute the next command 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);
                }
            }
        }
    }

粘貼應該做一些代碼,然后在那之后我應該刷新顯示的列表..我需要這樣做,因為當我在線程中調用刷新時,它對我不起作用
我該怎么做 ??

您可以在線程實例上使用Join方法,該方法將阻塞調用線程,直到另一個線程完成。

您還可以使用WaitHandles並使用WaitOneWaitAll方法。

i need to do it this way, because it doesn't work for me when i call the refresh in the thread

您的問題與JoinWaitWaitOne等線程同步無關。

從Windows Forms中的輔助線程更新用戶界面元素非常棘手,因為不允許輔助線程直接從表單或其任何子控件讀取或寫入屬性值。 存在此限制是因為Windows窗體中的窗體對象和控件對象不是線程安全的。 唯一可以直接訪問表單或其控件之一的屬性值的線程是主UI線程

要從線程(其他主線程)更新GUI,您需要Invoke窗體(或某些控件)的InvokeBeginInvoke方法,以將委托插入UI線程的Dispatcher中。

這些鏈接可以為您提供幫助。

如何從C#中的另一個線程更新GUI?

從輔助線程更新UI

從另一個線程更新GUI很容易

暫無
暫無

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

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