I work with new Parallel.For that creates multiple threads to perform the same operation.
In case one of the threads fail, it means that I’m working “too fast” and I need to put all the threads to rest for a few seconds.
Is there a way to perform something like Thread.Sleep – only to do the same on all threads at once?
This is a direct answer to the question, except for the
Parallel.Forbit.It really is a horrible pattern; you should probably be using a proper synchronization mechanism, and get the worker threads to, without preemption, occasionally check if they need to ‘back off.’
In addition, this uses
Thread.SuspendandThread.Resumewhich are both deprecated, and with good reason (from Thread.Suspend):“Do not use the Suspend and Resume methods to synchronize the activities of threads. You have no way of knowing what code a thread is executing when you suspend it. If you suspend a thread while it holds locks during a security permission evaluation, other threads in the AppDomain might be blocked. If you suspend a thread while it is executing a class constructor, other threads in the AppDomain that attempt to use that class are blocked. Deadlocks can occur very easily.”
(Untested)