1

Here the code Snippet, I want to exit from the function when the condition meets. How can i achieve this?

bool MyNestedFunction()
{
   Parallel.Foreach (ListofStrings_A, OuterString =>//loopA
   {

       Parallel.Foreach (ListofStrings_B, InnerString //loopB
       {
          string FirstTemperedString = Temperedstring.Substring(0,Temperedstring.LastIndexOf(" "));
          string LastTemperedString = Temperedstring.Substring(Temperedstring.IndexOf(" "));

          string FirstOuterString = OuterString.Substring(0, OuterString.LastIndexOf(" "));
          string LastOuterString = OuterString.Substring( OuterString.IndexOf(" "));

          if (FirstTemperedString.Equals(FirstOuterString) || LastTemperedString.Equals(LastOuterString))
          {
                return true; // I want to return from the function (MyNestedFunction)here
                // What will be the alternate code here
          }
        });
    })

    return false;
}

What will be the alternative statement for return true, that will exit this function?

4
  • 4
    I think you misunderstand what Parallel.Foreach does Commented Sep 9, 2013 at 13:31
  • @Sayse perhaps you could hint him to the right direction then? This isn't very constructive. Commented Sep 9, 2013 at 14:45
  • @C.Evenhuis - I don't know what direction the OP needs to go in so how can I point them in a direction? - OP, Parallel.Foreach will run the iterations of the foreach loop in a parallel fashion Commented Sep 9, 2013 at 14:59
  • 1
    @Sayse I just meant that only saying "I think you're misunderstanding" doesn't contribute, and is perhaps just as useful as saying nothing (although you did get two upvotes :) ) Commented Sep 9, 2013 at 17:37

1 Answer 1

4

As the two loops can be executed on different threads you have to introduce shared state between the controlling thread and the running threads handling the ForEach calls. In this case a boolean can be assumed to be atomic updateable and therefor threadsafe so no locking is needed.

In the if condition set the result bool to true and then call state.Stop(); to signal that the outerloop should end. When it does control is returned to the calling thread while result is now true and that value is returned to the caller.

bool MyNestedFunction()
{
   bool result = false; // shared state!

   Parallel.ForEach (ListofStrings_A, (OuterString, state) =>//loopA
   {

       Parallel.ForEach (ListofStrings_B, InnerString => //loopB
       {
          string FirstTemperedString = Temperedstring.Substring(0,Temperedstring.LastIndexOf(" "));
          string LastTemperedString = Temperedstring.Substring(Temperedstring.IndexOf(" "));

          string FirstOuterString = OuterString.Substring(0, OuterString.LastIndexOf(" "));
          string LastOuterString = OuterString.Substring( OuterString.IndexOf(" "));


          if (FirstTemperedString.Equals(FirstOuterString) || LastTemperedString.Equals(LastOuterString))
          {
                result = true; // set the return value
                state.Stop(); // signal to Stop the ForEach on all Threads
          }
        });
    });

    return result;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.