[英]how do i wait till the thread complete its job then execute the next command c#?
I have the code below (but doesn't work) .. i need to wait till the thread complete its job then execute then next command 我有下面的代码(但不起作用)..我需要等到线程完成其工作,然后执行然后执行下一条命令
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);
}
}
}
}
the paste should do some code then after that i should refresh the list im showing.. i need to do it this way, because it doesn't work for me when i call the refresh in the thread 粘贴应该做一些代码,然后在那之后我应该刷新显示的列表..我需要这样做,因为当我在线程中调用刷新时,它对我不起作用
how do i do it ?? 我该怎么做 ??
您可以在线程实例上使用Join方法,该方法将阻塞调用线程,直到另一个线程完成。
您还可以使用WaitHandles并使用WaitOne或WaitAll方法。
i need to do it this way, because it doesn't work for me when i call the refresh in the thread
Your problem is not related with thread synchronizations like Join
, Wait
, WaitOne
etc. 您的问题与Join
, Wait
, WaitOne
等线程同步无关。
Updating user interface elements from a secondary thread in Windows Forms is tricky because a secondary thread is not allowed to read or write property values directly from a form or any of its child controls. 从Windows Forms中的辅助线程更新用户界面元素非常棘手,因为不允许辅助线程直接从表单或其任何子控件读取或写入属性值。 This restriction exists because form objects and control objects in Windows Forms are not thread-safe. 存在此限制是因为Windows窗体中的窗体对象和控件对象不是线程安全的。 The only thread that's allowed to directly access a property value of a form or one of its controls is the primary UI thread 唯一可以直接访问表单或其控件之一的属性值的线程是主UI线程
To update your GUI from a thread (other your main thread), you need to call Invoke
or BeginInvoke
methods of your form(or some controls) to insert delegates into the Dispatcher of the UI thread. 要从线程(其他主线程)更新GUI,您需要Invoke
窗体(或某些控件)的Invoke
或BeginInvoke
方法,以将委托插入UI线程的Dispatcher中。
These links can help you. 这些链接可以为您提供帮助。
How to update GUI from another thread in C#? 如何从C#中的另一个线程更新GUI?
Updating the UI from a Secondary Thread 从辅助线程更新UI
Updating the GUI from another thread made easy 从另一个线程更新GUI很容易
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.