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?

rene
  • 41,474
  • 78
  • 114
  • 152
  • 4
    I think you misunderstand what `Parallel.Foreach` does – Sayse Sep 09 '13 at 13:31
  • @Sayse perhaps you could hint him to the right direction then? This isn't very constructive. – C.Evenhuis Sep 09 '13 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 – Sayse Sep 09 '13 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 :) ) – C.Evenhuis Sep 09 '13 at 17:37

1 Answers1

3

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;
}
Community
  • 1
  • 1
rene
  • 41,474
  • 78
  • 114
  • 152