简体   繁体   English

如何在不轮询ThreadState的情况下判断线程是否已完成执行?

[英]How can I tell if a thread is finished executing without polling ThreadState?

Is there an elegant way to know when a worker thread is done executing so I can access resources it produced? 是否有一种优雅的方式来了解工作线程何时完成执行,以便我可以访问它生成的资源?

For example if the worker thread queried a list of SQL Servers using 例如,如果工作线程使用查询SQL Server列表

ServersSqlDataSourceEnumerator.Instance.GetDataSources();

and saved the result in a DataTable variable, what mechanism can I use to know when this DataTable variable has been populated/is available. 并将结果保存在DataTable变量中,我可以使用什么机制来了解此DataTable变量何时已填充/可用。 I don't want to poll ThreadState; 我不想轮询ThreadState; it would be ideal to fire an event when it's done so I can perform actions with the result. 当事件完成时触发事件是理想的,这样我就可以对结果执行操作。

Thanks! 谢谢!

You can use a callback mechanism or block on an event to know of completion of an Async operation. 您可以在事件上使用回调机制或阻止来了解异步操作的完成情况。 See this page for the Asychronous Programming Model in .net - you can call BeginInvoke on any delegate to perform the action in an Async manner. 有关.net中异步编程模型,请参阅此页面 - 您可以在任何委托上调用BeginInvoke以异步方式执行操作。

If you're using the BackgroundWorker type, you can subscribe to the RunWorkerCompleted event . 如果您使用的是BackgroundWorker类型,则可以订阅RunWorkerCompleted事件

So fire an event :-P 所以发动一个事件:-P

You could also look at using an AutoResetEvent: 您还可以查看使用AutoResetEvent:
http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx

What I do in this instance is get the WorkerThread to call a function after it has completed the work, which will invoke the the UI Thread, which can do the work in which you require. 我在这个例子中做的是让WorkerThread在完成工作后调用一个函数,它将调用UI Thread,它可以完成你需要的工作。

Eg 例如

private void SetWorkerThreadToDoWork()
{
  WorkerThread.Start();
}

private void MyWorkerThreadWork()
{
  //This will be on the WorkerThread (called from WorkerThread.Start())
  DoWorkFunc();
  WorkComplete();
}

private void WorkComplete()
{
  if(InvokeRequired == true)
  {
    //Do the invoke
  }
  else
  {
  //Check work done by worker thread
  //e.g. ServersSqlDataSourceEnumerator.Instance.GetDataSources();
  }
}

If it's a simple process you're using, I'd go for a BackgroundWorkerThread, this comes with it's own events that are fired when work is complete. 如果这是一个你正在使用的简单过程,我会选择一个BackgroundWorkerThread,它带有它自己在工作完成时触发的事件。 But if you require to use a Thread, I would either look in to Asynchronous Callbacks or a similar route to that shown above. 但是如果你需要使用一个Thread,我会查看Asynchronous Callbacks或类似于上面显示的路径。

You can check my answer on this SO thread 你可以在这个SO线程上查看我的答案

It uses a call back mechanism. 它使用回调机制。 When the async operation is done, it will fire the callback method where you can handle the processing that needs to be done post SQL execution. 异步操作完成后,它将触发回调方法,您可以在其中处理SQL执行后需要完成的处理。

Use a similar approach to be notified when the asynchronous operation is done. 在异步操作完成时,使用类似的方法通知。

Hope this helps :) 希望这可以帮助 :)

I don't program in C# but here's what I did with Delphi, maybe you can do it as well with C#. 我不用C#编程,但这是我用Delphi做的,也许你可以用C#做​​。 I have a TThread descendant, and in the "destroy" event I send a message to its creator saying "hey I'm about to die !". 我有一个TThread后代,在“破坏”事件中,我向其创建者发送一条消息,说“嘿,我快死了!”。 This way its parent (which is the main thread) creates a new one if it needs a new one. 这样它的父(它是主线程)如果需要一个新线程就会创建一个新线程。 To be precise it launches a timer that, when fired, creates a new thread if a new one is needed (sites sucking time (lol) !!). 确切地说,它启动一个计时器,当被触发时,如果需要一个新的线程,则会创建一个新的线程(网站吸时间(lol)!!)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用线程池时,如何知道线程何时完成? - How can I tell when a thread have finished when using a thread pool? HttpWebRequest和长轮询:如何确定长轮询连接是否已建立? - HttpWebRequest & Long Polling: how can I tell if a Long Polling connection has been established? 在执行下一行代码之前,如何在不使线程进入睡眠状态的情况下等待计时器完成? - How can I wait for a timer to finished before I execute next line of code without putting the thread to sleep? 如何判断文件是否已完成FTP? - How can I tell if a file has finished being FTPed? 我如何判断一个数字何时完成增加? - How can I tell when a number has finished increasing? 我如何知道线程的状态? - How can i tell state of a thread? 如何告诉WPF UI线程工作者已完成工作? - How to tell WPF UI thread that worker has finished its job? Thread.IsAlive和Thread.ThreadState == ThreadState.Running - Thread.IsAlive and Thread.ThreadState==ThreadState.Running 我怎么知道tomcat何时开始启动? (使用C#控制服务) - How can I tell when tomcat is finished starting? (controlling the service with C#) NetworkStream.Write 立即返回 - 我如何知道它何时完成发送数据? - NetworkStream.Write returns immediately - how can I tell when it has finished sending data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM