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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:35:17+00:00 2026-05-19T04:35:17+00:00

On the subject of waiting until tasks are complete and thread synchronisation. I currently

  • 0

On the subject of waiting until tasks are complete and thread synchronisation.

I currently have an iteration i have enclosed within a Parallel.ForEach. In the Example below I have posed some questions in the comments about how best to handle the graceful termination of the loop (.NET 4.0);

private void myFunction()
    {

        IList<string> iListOfItems = new List<string>();
        // populate iListOfItems

        CancellationTokenSource cts = new CancellationTokenSource();

        ParallelOptions po = new ParallelOptions();
        po.MaxDegreeOfParallelism = 20; // max threads
        po.CancellationToken = cts.Token;

        try
        {
            var myWcfProxy = new myWcfClientSoapClient();

            if (Parallel.ForEach(iListOfItems, po, (item, loopsate) =>
            {
                try
                {
                    if (_requestedToStop)
                        loopsate.Stop();
                    // long running blocking WS call, check before and after
                    var response = myWcfProxy.ProcessIntervalConfiguration(item);
                    if (_requestedToStop)
                        loopsate.Stop();

                    // perform some local processing of the response object
                }
                catch (Exception ex)
                {
                    // cannot continue game over.
                    if (myWcfProxy.State == CommunicationState.Faulted)
                    {
                        loopsate.Stop();
                        throw;
                    }
                }

                // else carry on..
                // raise some events and other actions that could all risk an unhanded error.

            }
            ).IsCompleted)
            {
                RaiseAllItemsCompleteEvent();
            }
        }
        catch (Exception ex)
        {
            // if an unhandled error is raised within one of the Parallel.ForEach threads, do all threads in the
            // ForEach abort? or run to completion? Is loopsate.Stop (or equivalent) called as soon as the framework raises an Exception?
            // Do I need to call cts.Cancel here?

            // I want to wait for all the threads to terminate before I continue at this point. Howe do we achieve that?

            // do i need to call cts.Dispose() ?

            MessageBox.Show(Logging.FormatException(ex));
        }
        finally
        {

            if (myWcfProxy != null)
            {
            // possible race condition with the for-each threads here unless we wait for them to terminate.
                if (myWcfProxy.State == System.ServiceModel.CommunicationState.Faulted)
                    myWcfProxy.Abort();

                myWcfProxy.Close();
            }

            // possible race condition with the for-each threads here unless we wait for them to terminate.
            _requestedToStop = false;

        }

    }

Any help would be most appreciated. The MSDN documentation talks of ManualResetEventSlim’s and cancellationToken.WaitHandle’s. but not sure how to wire them into this, seem to be struggling understanding the MSDN examples as most dont apply.

  • 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-19T04:35:18+00:00Added an answer on May 19, 2026 at 4:35 am

    I have mocked up some code below that may answer your question. The basic point is that you get fork/join parallelism with Parallel.ForEach, so you don’t need to worry about race conditions outside of the parallel task (the calling thread blocks until the tasks have completed, successfully or otherwise). You just want to make sure to use the LoopState variable (the second argument to the lambda) to control your loop state.

    If any iteration of the loop threw an unhandled exception, the overall loop will raise the AggregateException caught at the end.

    Other links that mention this topic:

    Parallel.ForEach throws exception when processing extremely large sets of data

    http://msdn.microsoft.com/en-us/library/dd460720.aspx

    Does Parallel.ForEach limits the number of active threads?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.ServiceModel;
    
    namespace Temp
    {
        public class Class1
        {
            private class MockWcfProxy
            {
                internal object ProcessIntervalConfiguration(string item)
                {
                    return new Object();
                }
    
                public CommunicationState State { get; set; }
            }
    
            private void myFunction()
            {
    
                IList<string> iListOfItems = new List<string>();
                // populate iListOfItems
    
                CancellationTokenSource cts = new CancellationTokenSource();
    
                ParallelOptions po = new ParallelOptions();
                po.MaxDegreeOfParallelism = 20; // max threads
                po.CancellationToken = cts.Token;
    
                try
                {
                    var myWcfProxy = new MockWcfProxy();
    
                    if (Parallel.ForEach(iListOfItems, po, (item, loopState) =>
                        {
                            try
                            {
                                if (loopState.ShouldExitCurrentIteration || loopState.IsExceptional)
                                    loopState.Stop();
    
                                // long running blocking WS call, check before and after
                                var response = myWcfProxy.ProcessIntervalConfiguration(item);
    
                                if (loopState.ShouldExitCurrentIteration || loopState.IsExceptional)
                                    loopState.Stop();
    
                                // perform some local processing of the response object
                            }
                            catch (Exception ex)
                            {
                                // cannot continue game over.
                                if (myWcfProxy.State == CommunicationState.Faulted)
                                {
                                    loopState.Stop();
                                    throw;
                                }
    
                                // FYI you are swallowing all other exceptions here...
                            }
    
                            // else carry on..
                            // raise some events and other actions that could all risk an unhanded error.
                        }
                    ).IsCompleted)
                    {
                        RaiseAllItemsCompleteEvent();
                    }
                }
                catch (AggregateException aggEx)
                {
                    // This section will be entered if any of the loops threw an unhandled exception.  
                    // Because we re-threw the WCF exeption above, you can use aggEx.InnerExceptions here 
                    // to see those (if you want).
                }
                // Execution will not get to this point until all of the iterations have completed (or one 
                // has failed, and all that were running when that failure occurred complete).
            }
    
            private void RaiseAllItemsCompleteEvent()
            {
                // Everything completed...
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently involved in some interesting programming language research which has, up until
I'm currently building a class using TDD. The class is responsible for waiting for
Is the subject possible? I have a script executing. At one point I have
I know this is a controversial subject and I know my reasons for wanting
Subject says it all really...Is there anywhere a good tutorial for Xcode's Debugger out
Subject of discussion: User security on non rooted Android phones. Is it possible to
The subject pretty much says it all. How does the meta:resourcekey attribute work, and
The subject line says it all. I'd also like to do this using pipes.
The subject says it all, really. Documentation, insofar as it exists at all, suggests
Exactly as the subject, How to create oval button in WPF application?

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.