views:

463

answers:

3

Hi guys it's me again this is my Problem : I have a stack of threads , each thread will do its job and then return to the stack at the first time the thread is working well after it finishes the job if I want to pop it from the stack to run again : Exception will appear which said this thread is running or terminated can't be restarted .... is it allowed to run the thread more than one time ?? thanks ...

A: 

Are you using the built-in ThreadPool object? This can be the best option for allocating and reusing threads.

ck
I just define the threads and then push them into the StackCan you explain more how to use the threadpool ??
Hany
+3  A: 

You cannot run a thread more than once. Once a thread finishes, it's done.

If you want to reuse threads, your best option is to use the Framework's ThreadPool. It will automatically handle taking your "work" and pushing it into a free thread, scheduling, and queuing tasks that can't be run because you're using all of the available threads.

Reed Copsey
thanks my friend Reed I will check The threaPool
Hany
+3  A: 

Sounds to me like you're trying to implement your own thread pool. Try using System.Threading.ThreadPool instead.

Next, convert your Stack{Thread} to a Stack{Action}. Pop items off the stack, run them using the built-in threadpool, then return items to the stack as needed.

Juliet