locked
Parallel.Foreach RRS feed

  • Question

  • I would like to replace Foreach loop with "continue"  stmt to Parallel.Foreach . How  can continue will be replaced in Parallel.Foreach


    Prasanna119
    Wednesday, January 11, 2012 6:57 PM

Answers

  • Given that the member of the body is a function you can use the return statement.
    • Proposed as answer by Nick Bray Thursday, January 12, 2012 4:39 PM
    • Marked as answer by Paul Zhou Thursday, January 19, 2012 7:16 AM
    Wednesday, January 11, 2012 7:08 PM
  • Yes, the poster above was correct.

    Here is an example:

    static void Main(string[] args)
            {
                string[] inStrings = new string[] {"A", "B", "C"};
                List<string> outList1 = new List<string>();
                List<string> outList2 = new List<string>();
    
                //non parallel version
                foreach(string s in inStrings)
                {
                      if(string.Equals(s,"A"))
                          continue;
    
                       outList1.Add(s);     
                }
    
                foreach (string s in outList1)
                {
                    Console.Write(s + " ");
                }
                Console.WriteLine();
    
                //parallel version that removes A also, but order may be different
                System.Threading.Tasks.Parallel.ForEach<string>(inStrings, s =>
                    {
                        if (string.Equals(s, "A"))
                            return;
    
                        outList2.Add(s);
                    }
                );
    
                foreach (string s in outList1)
                {
                    Console.Write(s + " ");
                }
                Console.WriteLine();
    
                Console.Read();
            }
    

    If this works, please mark the post above as the correct answer.

    • Marked as answer by Paul Zhou Thursday, January 19, 2012 7:16 AM
    Thursday, January 12, 2012 5:44 AM

All replies

  • Given that the member of the body is a function you can use the return statement.
    • Proposed as answer by Nick Bray Thursday, January 12, 2012 4:39 PM
    • Marked as answer by Paul Zhou Thursday, January 19, 2012 7:16 AM
    Wednesday, January 11, 2012 7:08 PM
  •  

     

    List<string> userNames = new List<string>();

    Foreach(string s in strArr)

    {

          If(string.Equals(s,"Prasanna"))

              continue;

           userNames.Add(s);     

     

    }

    How to replace "continue" as above in Parallel.Foreach . Can i use "return" instead of continue in Parallel.Foreach

     


    Prasanna119
    Wednesday, January 11, 2012 7:25 PM
  • Yes, the poster above was correct.

    Here is an example:

    static void Main(string[] args)
            {
                string[] inStrings = new string[] {"A", "B", "C"};
                List<string> outList1 = new List<string>();
                List<string> outList2 = new List<string>();
    
                //non parallel version
                foreach(string s in inStrings)
                {
                      if(string.Equals(s,"A"))
                          continue;
    
                       outList1.Add(s);     
                }
    
                foreach (string s in outList1)
                {
                    Console.Write(s + " ");
                }
                Console.WriteLine();
    
                //parallel version that removes A also, but order may be different
                System.Threading.Tasks.Parallel.ForEach<string>(inStrings, s =>
                    {
                        if (string.Equals(s, "A"))
                            return;
    
                        outList2.Add(s);
                    }
                );
    
                foreach (string s in outList1)
                {
                    Console.Write(s + " ");
                }
                Console.WriteLine();
    
                Console.Read();
            }
    

    If this works, please mark the post above as the correct answer.

    • Marked as answer by Paul Zhou Thursday, January 19, 2012 7:16 AM
    Thursday, January 12, 2012 5:44 AM