Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have 2 for loops in a function, doing different jobs and running one after another. How can I run them in parallel(both loops do there jobs at same time) ?

public void func()
{
   for(i=0;i<10;i++)
   {

   }

   for (j=0;j<10;j++)
   {

   }
}


What I have tried:

I have 2 for loops in a function, doing different jobs and running one after another. How can I run them in parallel(both loops do there jobs at same time) ?
Posted
Updated 3-Mar-18 0:03am
v2

public void func()
{
    // This code is on thread 1

    Thread ta = new Thread(new ThreadStart(LoopA)); // create thread 2 for LoopA
    Thread tb = new Thread(new ThreadStart(LoopB)); // create thread 3 for LoopB

    ta.Start(); // Run LoopA on thread 2
    tb.Start(); // Run LoopB on thread 3

    // Join makes this thread wait until the thread being joined to has finished

    ta.Join(); // wait for thread 2 to finish
    tb.Join(); // now wait for thread 3 to finish

    // From this point on we know that both loops have completed
}

public void LoopA()
{
    for (int i = 0; i < 1000; i++)
    {
        Console.WriteLine("i = " + i.ToString());
    }
}

public void LoopB()
{
    for (int j = 0; j < 1000; j++)
    {
        Console.WriteLine("j = " + j.ToString());
    }
}
 
Share this answer
 
Comments
Akhil Jain 27-Feb-18 21:45pm    
can't this be done in Func alone(all the Loop are in func as per the Question) ?
Dave Kreskowiak 1-Mar-18 8:56am    
No, it can't be done like that.
F-ES Sitecore 1-Mar-18 9:07am    
You can use anonymous methods rather than concrete functions but it's not as readable and only really suitable if the code is only a few lines

Thread ta = new Thread(new ThreadStart(() =>
{
for (int i = 0; i < 1000; i++)
{
Console.WriteLine("i = " + i.ToString());
}
}));

If you're asking is there some kind of simple keyword that tells the code to run in parallel then there isn't.
What about multiple threads (see, for instance Multi-threading in .NET: Introduction and suggestions[^])?
 
Share this answer
 

What about employing the Task-Based Asynchronous Pattern?


C#
private static async Task MainAsync()

    {
        await MyMethodAsync();
    }

private static async Task MyMethodAsync()
    {
        //start the tasks
        const int count = 10;
        Task<string> loopATask = Task.Run(() => LoopA(count));
        Task<string> loopBTask = Task.Run(() => LoopB(count));
        //await them to complete
        string resultA = await loopATask;
        string resultB = await loopBTask;
        Console.WriteLine(resultA + resultB);
    }


    public static string LoopA(int count)
    {
        for (int i = 0; i < count; i++)
        {
            Console.WriteLine("LoopA " + i);
        }
        return "LoopA has finished ";
    }

    public static string LoopB(int count)
    {
        for (int i = 0; i < count; i++)
        {
            Console.WriteLine("LoopB " + i);
        }
        return "LoopB has finished ";
    }
 
Share this answer
 
If you meant:
public void func()
{
   for(i=0;i<10;i++)
   {
        aaa();
        bbb();
        ccc();
   }

   for (i=0;i<10;i++)
   {
       yyy();
       zzz();
   }
}


then it can be replaced to :
public void func()
{
   for(i=0;i<10;i++)
   {
        aaa();
        bbb();
        ccc();

        yyy();
        zzz();
   }

}
 
Share this answer
 
Comments
Richard MacCutchan 2-Mar-18 7:52am    
That is not running them at the same time.
Ashok Kumar RV 2-Mar-18 11:34am    
Anyone learned first lesson in multithreading will be able to this task. Obviously he has to study multithreading. Since his question seemed innocent i hoped my answer would suffice.
Richard MacCutchan 2-Mar-18 12:14pm    
Your "answer" has nothing to do with threading, and gives no suggestions that would help someone new to the subject.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900