0

I'm developing a software which I need to make parallel tasks. For example here is my task1:

Task t1 = new Task(()=>
{
    //Do Some Codes
});

And I have more tasks which have the same code as task1:

Task t2 = new Task(()=>
{
    //Do task1 codes
});

Task t3 = new Task(()=>
{
    //Do task1 codes
});

t1.start();
t2.start();
t3.start();

I also have a timer so that I can check these tasks works and do the processes faster. But it won't change and they have the same time as one task.

Now I want to know how I can run parallel tasks and give me faster result.

Any help will be appreciated

Masoud Keshavarz
  • 2,166
  • 9
  • 36
  • 48
Ali Vojdanian
  • 2,067
  • 2
  • 31
  • 47

2 Answers2

0

Let's suppose you have two functions and you want to execute them parallel. I think there are two ways you can do that:

1: Using Task Parallel Library-

private void Function1()
{
    //do work
}

private void Function2()
{
    //do work
}

private void RunParallel()
{
    Parallel.Invoke(Function1, Function2);
}

2: Running each function in separate threads-

private void Function1()
{
    //do work
}

private void Function2()
{
    //do work
}

private void RunParallel()
{
    ThreadStart ts1 = new ThreadStart(Function1);
    ThreadStart ts2 = new ThreadStart(Function2);

    Thread t1 = new Thread(ts1);
    Thread t2 = new Thread(ts2);

    t1.Start();
    t2.Start();
}
Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
-1

You can try to use "Multiple Threads" for calling your Methods or calling async methods. That should help.

For More Info:

https://learn.microsoft.com/en-us/dotnet/csharp/async

And the voted answer on this question can be helpful too

What is the difference between asynchronous programming and multithreading?

Zilk
  • 1
  • 1
  • 1