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

  • SEARCH
  • Home
  • 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 6928307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:10:12+00:00 2026-05-27T11:10:12+00:00

What would be the most effective way to pause and stop (before it ends)

  • 0

What would be the most effective way to pause and stop (before it ends) parallel.foreach?

Parallel.ForEach(list, (item) =>
{
    doStuff(item);
});
  • 1 1 Answer
  • 0 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-27T11:10:13+00:00Added an answer on May 27, 2026 at 11:10 am

    Damien_The_Unbeliver has a good method, but that is only if you want to have some outside process stop the loop. If you want to have the loop break out like using a break in a normal for or foreach loop you will need to use a overload that has a ParallelLoopState as one of the parameters of the loop body. ParallelLoopState has two functions that are relevant to what you want to do, Stop() and Break().

    The function Stop() will stop processing elements at the system’s earliest convenience which means more iterations could be performed after you call Stop() and it is not guaranteed that the elements that came before the element you stopped at have even begun to process.

    The function Break() performs exactly the same as Stop() however it will also evaluate all elements of the IEnumerable that came before the item that you called Break() on. This is useful for when you do not care what order the elements are processed in, but you must process all of the elements up to the point you stopped.

    Inspect the ParallelLoopResult returned from the foreach to see if the foreach stopped early, and if you used Break(), what is the lowest numbered item it processed.

    Parallel.ForEach(list, (item, loopState) =>
        {
            bool endEarly = doStuff(item);
            if(endEarly)
            {
                loopState.Break();
            }
        }
        );
    //Equivalent to the following non parallel version, except that if doStuff ends early
    //    it may or may not processed some items in the list after the break.
    foreach(var item in list)
    {
        bool endEarly = doStuff(item);
        if(endEarly)
        {
            break;
        }
    }
    

    Here is a more practical example

    static bool[] list = new int[]{false, false, true, false, true, false};
    
    long LowestElementTrue()
    {
        ParallelLoopResult result = Parallel.ForEach(list, (element, loopState) =>
        {
            if(element)
                loopState.Break();
        }
        if(result.LowestBreakIteration.IsNull)
            return -1;
        else
            return result.LowestBreakIteration.Value;
    }   
    

    No matter how it splits up the work it will always return 2 as an answer.

    let’s say the processor dispatches two threads to process this, the first thread processes elements 0-2 and the 2nd thread processes elements 3-5.

    Thread 1:                Thread 2
    0, False, continue next  3, False, continue next
    1, False, continue next  4, True, Break
    2, True, Break           5, Don't process Broke
    

    Now the lowest index Break was called from was 2 so ParallelLoopResult.LowestBreakIteration will return 2 every time, no-matter how the threads are broken up as it will always process up to the number 2.

    Here an example how how Stop could be used.

    static bool[] list = new int[]{false, false, true,  false, true, false};
    
    long FirstElementFoundTrue()
    {
        long currentIndex = -1;
        ParallelLoopResult result = Parallel.ForEach(list, (element, loopState, index) =>
        {
            if(element)
            {
                 loopState.Stop();
    
                 //index is a 64 bit number, to make it a atomic write
                 // on 32 bit machines you must either:
                 //   1. Target 64 bit only and not allow 32 bit machines.
                 //   2. Cast the number to 32 bit.
                 //   3. Use one of the Interlocked methods.
                 Interlocked.Exchange (ref currentIndex , index);
            }
        }
        return currentIndex;
    }   
    

    Depending how it splits up the work it will either return 2 or 4 as the answer.

    let’s say the processor dispatches two threads to process this, the first thread processes elements 0-2 and the 2nd thread processes elements 3-5.

    Thread 1:                 Thread 2
    0, False, continue next    3, False, continue next
    1, False, continue next    4, True, Stop
    2, Don't process, Stopped  5, Don't process, Stopped
    

    In this case it will return 4 as the answer. Lets see the same process but if it processes every other element instead of 0-2 and 3-5.

    Thread 1:                   Thread 2
    0, False, continue next     1, False, continue next
    2, True, Stop               3, False, continue next
    4, Don't process, Stopped   5, Don't process, Stopped
    

    This time it will return 2 instead of 4.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question: What would be the most effective way of doing a nested list which
What would be the most effective way to grab the schema + table name
I would like to know the simplest and most effective way to open and
what would be the most effective way to implement search for a string in
What would be the most effective way (that is, best ratio of effort vs
what would you recommend to be the most effective way to load a pdf
What would be the most effective way to parse the hour and AM/PM value
For something like this: What would be the most effective way to do this?
What would be the most efficient way of recording to a log (.txt) from
What would be the most idiomatic way to do the following in JavaScript: If

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.