c# - Thread: How to re-start thread once completed? -
i have method void dowork(object input)
takes 5 seconds complete. have read thread
better suited threadpool
these longer operations have encountered problem.
i click button calls threadrun.start(input)
runs , completes fine. click button again , receive following exception:
thread running or terminated; cannot restart.
can not "reuse" thread? should use threadpool? why thread "better suited longer operations" compared threadpool? if can't reuse thread, why use @ (i.e. advantages offer)?
can not "reuse" thread?
you can. have code thread not terminate instead wait more work. that's thread pool does.
should use threadpool?
if want re-use thread, yes.
why thread "better suited longer operations" compared threadpool?
imagine thread pool serving large number of quick operations. don't want have many threads, because computer can many things @ time. each long operation make thread pool ties thread pool. pool either has have lots of threads or may run short of threads. neither leads efficient thread pool design.
for longer operations, overhead of creating , destroying thread small in comparison cost of operation. normal downside of using thread operation doesn't apply.
if can't reuse thread, why use @ (i.e. advantages offer)?
i'm assuming mean using thread dedicated job terminates on using thread pool. advantage number of threads equal number of jobs way. means have create thread every time start job , destroy thread every time finish one, never have threads nor ever run short on threads. (this can thing i/o bound threads can bad thing if threads cpu bound of time.)
Comments
Post a Comment