且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#中的线程查询

更新时间:2023-02-08 16:12:43

写道:​​

我怎么知道线程已经完成了工作?

How do i know that the thread has finished the job?



正如我之前所说,您可以调用Thread.IsAliveThread.ThreadState(这将提供更多信息).




As I said before, you can call Thread.IsAlive or Thread.ThreadState ( which gives more information ).


写道:​​

我是否需要像处理对象一样中止或处置线程?

Do i need to abort or dispose the thread just like we do for objects?



Thread没有实现IDisposable,因此您无法处理它.

Thread.Abort邪恶-永远不要使用它!

因此,您的问题的答案是否定的.线程完成后,只需删除对其的所有引用,运行时将为您清理.



我建议您在控件类中保留对线程的引用,并在计时器触发时检查以前的线程是否已完成.如果它们仍在工作,则返回并等待下一个计时器.如果完成,则可以再次创建新的文件.

Nick



Thread does not implement IDisposable, so you can''t dispose it.

Thread.Abort is evil - never use it!

So the answer to your question is no. When a thread finishes, just remove any references to it and the runtime will clean up for you.



I suggest that you keep references to your threads in your control class and when the timer fires, check to see if the previous threads have completed. If they are still working then just return and wait for the next timer. If they have finished, then you can create new ones again.

Nick


通常的做法和最简单的方法是在线程启动后,为每个新线程循环调用thread.Join().主线程将等待加入"线程完成,然后您可以通知用户.



您还可以通过ManualResetEvent
显式发出线程信号
The common practice and the easiest way is to call thread.Join() for each new thread in cycle after your threads have launched. The main thread will wait for the "joined" threads to be finished and then you can notify a user.



You also can signal threads explicitly via ManualResetEvent