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 7038009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:37:03+00:00 2026-05-28T01:37:03+00:00

I have a simple question, i have following simple Parallel for loop. this for

  • 0

I have a simple question, i have following simple Parallel for loop. this for loop is part of windows service. I want to stop the loop, when someone stops the service. I can find three ways to stop parallel for, which is in if condition. What is the best way of stopping the parallel for loop and what are the differences?

       CancellationTokenSource cancellationToken = new CancellationTokenSource();
       ParallelOptions options = new ParallelOptions();
       options.CancellationToken = cancellationToken.Token;

       Parallel.For(0, maximum_operations, options, (a, loopState) =>
        {
            {
                //Do something

                if(!KeepProcessing)
                { 
                    //loopState.Break();
                    //loopState.Stop();
                    cancellationToken.Cancel();

                }
            }
        });
  • 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-28T01:37:04+00:00Added an answer on May 28, 2026 at 1:37 am

    CancellationToken is used to signal cancellation.

    loopState.Break() and loopState.Stop() are used to end execution.

    Here’s an example

    Parallel.For(0, maximum_operations, options, (a, loopState) =>
        {
            // do work
    
            // cancellationToken.Cancel() should be called externally
            if(token.IsCancellationRequested)
            {
                // cancellation requested - perform cleanup work if necessary
    
                // then call
                loopState.Break();
                // or
                loopState.Stop();
            }
        });
    

    loopState.Break() means complete all iterations on all threads that are prior to the current iteration on the current thread, and then exit the loop (MSDN).

    loopState.Stop() means stop all iterations as soon as convenient (MSDN).


    Another way to terminate execution is call token.ThrowIfCancellationRequested(), but you will need to handle the OperationCanceledException exception:

    public void MyMethod()
    {
        try
        {
            Parallel.For(0, maximum_operations, options, (a, loopState) =>
            {
                // do work
    
                token.ThrowIfCancellationRequested();
            });
        }
        catch (OperationCanceledException)
        {
            // handle cancellation
        }
    }
    

    All of these methods are valid ways to terminate execution of Parallel.For. Which one you use depends on your requirements.

    For example:

    • Is it essential to immediately stop all execution when your windows Service is stopped? Then you could use token.ThrowIfCancellationRequested()
    • Does your loop deal with IDisposable objects that need cleanup? Then you could use loopState.Break() or loopState.Stop()

    Some articles for reference:

    • MSDN: Cancellation
    • MSDN: How to: Stop or Break from a Parallel.For Loop
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple question: I have the following markup... <a href='#'> <img src='icon.png'> This is the
Simple regex question. I have a string on the following format: this is a
I have this rather simple question about Scala. Given that i have to following
A little stuck here. I have a simple question I guess. Given the following
simple question :- i have the following simple if (..) statements :- if (foo
I have a very simple question, and I shouldn't be hung up on this,
I have a simple SQL question. I want to make a 3 column database
i have a propably rather simple question: I'm using the following script to read
Very simple question: suppose I have the following js/jquery code doSomething(); $.get(url, callback); doSomethingElse();
I have such a simple question, I feel stupid for asking it. I can't

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.