Thread: How to re-start thread once completed?

筅森魡賤 提交于 2019-12-10 02:15:01

问题


I have a method void DoWork(object input) that takes roughly 5 seconds to complete. I have read that Thread is better suited than ThreadPool for these longer operations but I have encountered a problem.

I click a button which calls threadRun.Start(input) which runs and completes fine. I click the button again and receive the following exception:

Thread is running or terminated; it cannot restart.

Can you not "reuse" a Thread? Should I use ThreadPool? Why is Thread "better suited for longer operations" compared to ThreadPool? If you can't reuse a thread, why use it at all (i.e. what advantages does it offer)?


回答1:


Can you not "reuse" a Thread?

You can. But you have to code the thread not to terminate but to instead wait for more work. That's what a thread pool does.

Should I use ThreadPool?

If you want to re-use a thread, yes.

Why is Thread "better suited for longer operations" compared to ThreadPool?

Imagine a thread pool that is serving a large number of quick operations. You don't want to have too many threads, because the computer can only do so many things at a time. Each long operation you make the thread pool do ties up a thread from the pool. So the pool either has to have lots of extra threads or may run short of threads. Neither leads to an efficient thread pool design.

For longer operations, the overhead of creating and destroying a thread is very small in comparison to the cost of the operation. So the normal downside of using a thread just for the operation doesn't apply.

If you can't reuse a thread, why use it at all (i.e. what advantages does it offer)?

I'm assuming you mean using a thread dedicated to a job that then terminates over using a thread pool. The advantage is that the number of threads will always equal the number of jobs this way. This means you have to create a thread every time you start a job and destroy a thread every time you finish one, but you never have extra threads nor do you ever run short on threads. (This can be a good thing with I/O bound threads but can be a bad thing if most threads are CPU bound most of the time.)




回答2:


Thread.Start documentation says:

Once the thread terminates, it cannot be restarted with another call to Start.

Threads are not reusable. I have already faced this problem a while ago, the solution was to create a new Thread instance whenever needed.




回答3:


It looks like this by by design.

I encountered the same problem and the only solution I could find was to recreate the thread. In my case I wasn't restarting the thread very often so I didn't look any further.

A search now has turned up this thread on social.msdn where the accepted answer states:

a stopped or aborted thread cannot be stated again.

The MSDN repeat this as well:

trying to restart an aborted thread by calling Start on a thread that has terminated throws a ThreadStateException.




回答4:


As the message states, you cannot restart the thread. You can simply create a new thread for your next operation. Or, you might consider a design where the background thread keeps working until it completes all of your tasks, rather than launch a new thread for each one.




回答5:


for(;;){} or while(true){} are useful constructs to 'reuse' a thread. Typically, the thread waits on some synchronization object at the top of these loops. In your example, you could wait on an event or semaphore and signal it from your button OnClick() handler.




回答6:


It's just in background mode. It sounds like you need to use the ThreadPool because re-starting and re-creating Thread objects are very expensive operations. If you have a long running job that may last longer than your main process, then consider the use of a Windows Service.



来源:https://stackoverflow.com/questions/9610226/thread-how-to-re-start-thread-once-completed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!