Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1019039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:56:57+00:00 2026-05-16T10:56:57+00:00

I work with new Parallel.For that creates multiple threads to perform the same operation.

  • 0

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?

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:56:57+00:00Added an answer on May 16, 2026 at 10:56 am

    This is a direct answer to the question, except for the Parallel.For bit.
    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.Suspend and Thread.Resume which 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)

    public class Worker
    {
        private readonly Thread[] _threads;
        private readonly object _locker = new object();
        private readonly TimeSpan _tooFastSuspensionSpan;
        private DateTime _lastSuspensionTime;
    
        public Worker(int numThreads, TimeSpan tooFastSuspensionSpan)
        {
            _tooFastSuspensionSpan = tooFastSuspensionSpan;
            _threads = Enumerable.Repeat(new ThreadStart(DoWork), numThreads)
                                 .Select(ts => new Thread(ts))
                                 .ToArray();
        }
    
        public void Run()
        {
            foreach (var thread in _threads)
            {
                thread.Start();
            }
        }
    
        private void DoWork()
        {
            while (!IsWorkComplete())
            {
                try
                {
                    // Do work here
    
                }
                catch (TooFastException)
                {
                    SuspendAll();
                }
            }
    
        }
    
        private void SuspendAll()
        {
            lock (_locker)
            {
                // We don't want N near-simultaneous failures causing a sleep-duration of N * _tooFastSuspensionSpan
                // 1 second is arbitrary. We can't be deterministic about it since we are forcefully suspending threads
                var now = DateTime.Now;
                if (now.Subtract(_lastSuspensionTime) < _tooFastSuspensionSpan + TimeSpan.FromSeconds(1))
                    return;
    
                _lastSuspensionTime = now;
    
                var otherThreads = _threads.Where(t => t.ManagedThreadId != Thread.CurrentThread.ManagedThreadId).ToArray();
    
                foreach (var otherThread in otherThreads)
                    otherThread.Suspend();
    
                Thread.Sleep(_tooFastSuspensionSpan);
    
                foreach (var otherThread in otherThreads)
                    otherThread.Resume();
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

let's say that you in your new programming work your boss comes and says
I am trying to develop an extension that will work with Awesome New Tab
I am working on a system that will analyze video frames from multiple cameras.
I understand that the new TPL (Task Parallel Library) has implemented the Parallel.ForEach such
I'm quite new to parallel programming and threads. I want to calculate and add
I have a need to create multiple processing threads in a new application. Each
I'm using Git on a new project that has two parallel -- but currently
I have a embarrassingly parallel problem that I want to execute on multiple processors.
I've created a new Work Item type for use in our TFS 2010 setup
i want to know how work facebook new timeline image reposition with jquery? this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.