我如何等到线程完成其工作然后执行下一个命令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);
}
}
}
}
粘贴应该执行一些代码,然后我应该刷新我显示的列表..我需要这样做,因为当我在线程中调用刷新时它对我不起作用
我该怎么做?
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 ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在线程实例上使用 Join 方法,该方法将阻止调用线程直到另一个线程完成。
You could use the Join method on the thread instance which will block the calling thread until the other thread has finished.
您还可以使用 WaitHandles 并使用 WaitOne 或 WaitAll 方法。
You can also use WaitHandles and use the WaitOne or WaitAll methods.
我需要这样做,因为当我在线程中调用刷新时它对我不起作用
您的问题与
Join
等线程同步无关,<代码>等待,<代码>WaitOne等。要从线程(其他主线程)更新 GUI,您需要调用
Invoke
或表单(或某些控件)的BeginInvoke
方法将委托插入到 UI 线程的 Dispatcher 中。这些链接可以帮助您。
如何从 C# 中的另一个线程更新 GUI?
从辅助线程更新 UI
从另一个线程更新 GUI 变得容易
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.To update your GUI from a thread (other your main thread), you need to call
Invoke
orBeginInvoke
methods of your form(or some controls) to insert delegates into the Dispatcher of the UI thread.These links can help you.
How to update GUI from another thread in C#?
Updating the UI from a Secondary Thread
Updating the GUI from another thread made easy