繁体   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