Subject: How to restart the stream after completion?

I have a void DoWork(object input) method that takes about 5 seconds. I read that Thread better than ThreadPool for these longer operations, but I ran into a problem.

I press a button that calls threadRun.Start(input) , which starts and ends normally. I press the button again and get the following exception:

Thread is running or terminated; it cannot restart.

Can't you reuse Thread? Should I use ThreadPool? Why is Thread “better suited for longer operations” compared to ThreadPool? If you cannot reuse a stream, why use it at all (that is, what advantages does it offer)?

+8
multithreading c #
source share
6 answers

Can't you reuse the stream?

You can. But you need to encode the stream so that it does not end, and instead wait for more work. This is what the thread pool does.

Should I use ThreadPool?

If you want to reuse the stream, yes.

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

Imagine a thread pool serving a large number of fast operations. You do not want to have too many threads, because the computer can do so many things at a time. Each long operation that you do with a thread pool binds a thread from the pool. Thus, the pool must have many additional threads or there may be fewer threads. None of them lead to the creation of an efficient pool of threads.

For longer operations, the overhead of creating and destroying a stream is very small compared to the cost of the operation. Thus, the normal disadvantage of using a thread for operation only is not applicable.

If you cannot reuse a stream, why use it at all (that is, what advantages does it offer)?

I assume that you are referring to the use of a thread dedicated to a task, which then completes using a thread pool. The advantage is that the number of threads will always be equal to the number of jobs in this way. This means that you need to create a thread every time you start a task, and destroy the thread every time you finish it, but you never have additional threads, and you never work with threads. (This may be good with I / O-related threads, but it may be bad if most threads are most commonly associated with CPUs.)

+10
source share

Thread.Start documentation says:

Once the thread completes, it cannot be restarted by another call to start.

Threads cannot be reused. I already encountered this problem some time ago, the solution was to create a new instance of Thread when necessary.

+3
source share

As the message says, you cannot restart the stream. You can simply create a new thread for the next operation. Or you can consider a design in which the background thread continues to work until it completes all your tasks, instead of starting a new thread for each of them.

+1
source share

It looks like a design.

I ran into the same problem, and the only solution I could find was to recreate the thread. In my case, I did not restart the thread very often, so I did not look anymore.

Now the search has found this thread in social.msdn , where the accepted answer says:

a stopped or interrupted thread cannot be claimed again.

MSDN repeat this:

attempting to restart an interrupted thread by calling "Start" on the thread that completed the throws of a ThreadStateException .

+1
source share

for(;;){} or while(true){} are useful constructs for "reusing" a stream. Typically, a thread expects some synchronization object at the top of these loops. In your example, you can wait for an event or semaphore and signal it using your OnClick() handler.

+1
source share

It is only in the background. It sounds like you need to use ThreadPool because restarting and re-creating Thread objects are very expensive operations. If you have a long job that can take longer than the main process, consider using the Windows service.

0
source share

All Articles