Can I run a Thread multiple times?
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 : Exceptio开发者_开发知识库n 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 ...
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.
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.
Are you using the built-in ThreadPool object? This can be the best option for allocating and reusing threads.
精彩评论