11

Could anyone please give a sample or any link that describes how to spawn thread where each will do different work at the same time.

Suppose I have job1 and job2. I want to run both the jobs simultaneously. I need those jobs to get executed in parallel. how can I do that?

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
deepak
  • 121
  • 1
  • 1
  • 5

3 Answers3

22

Well, fundamentally it's as simple as:

ThreadStart work = NameOfMethodToCall;
Thread thread = new Thread(work);
thread.Start();
...

private void NameOfMethodToCall()
{
    // This will be executed on another thread
}

However, there are other options such as the thread pool or (in .NET 4) using Parallel Extensions.

I have a threading tutorial which is rather old, and Joe Alabahari has one too.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • suppose I have job1 and job2. I want to run both the jobs simultaneously. Your example will do it sequentially. But I need those jobs to get executeded parallely. how can I do that ? – deepak Apr 18 '10 at 06:50
  • @deepak: No it won't - you start two threads, and they will run in parallel. That's the whole point of using threads. – Jon Skeet Apr 18 '10 at 06:53
  • @JonSkeet What if I wish to pass parameters to the method that I want to call? – Abijeet Patro Aug 01 '13 at 05:23
  • 1
    @AbijeetPatro: Then you either use `ParameterizedThreadStart` or use an anonymous function to capture the parameters. If that's not enough detail, please ask a separate question (after searching for answer - I suspect the question's been asked before). – Jon Skeet Aug 01 '13 at 05:52
  • @JonSkeet Yes it has been, thanks. To see how to call a method that has parameters through a thread, check here - http://stackoverflow.com/a/3360582/903324 – Abijeet Patro Aug 01 '13 at 06:47
  • Visual Basic and C# users can omit the `ThreadStart` or `ParameterizedThreadStart` delegate constructor when creating a thread. In C#, simply specify the name of the thread procedure. The compiler selects the correct delegate constructor. `Thread thread = new Thread(NameOfMethodToCall);` ***Also make sure your method is static:*** `private static void NameOfMethodToCall() { //code };` – spencer741 Nov 28 '19 at 07:16
  • @spencer741: It doesn't have to be static - method group conversions will work with instance methods so long as the context of the conversion is also an instance method. – Jon Skeet Nov 28 '19 at 08:08
  • @JonSkeet Isolating the code in my comment, the method would need to be static, correct? – spencer741 Nov 28 '19 at 08:41
  • @spencer741: No. If `Thread thread = new Thread(NameOfMethodToCall);` is executed within an instance method, it's fine for `NameOfMethodToCall` to be an instance method too. See https://dotnetfiddle.net/bVSOKI – Jon Skeet Nov 28 '19 at 08:52
1

Threading Tutorial from MSDN!

http://msdn.microsoft.com/en-us/library/aa645740(VS.71).aspx

Mahesh Velaga
  • 21,633
  • 5
  • 37
  • 59
  • Your link is (now) broken (download has been "retired") but I was able to find [link](https://msdn.microsoft.com/en-us/ie/aa645740(v=vs.94)) – Chad Aug 07 '19 at 03:01
0

Threads in C# are modelled by Thread Class. When a process starts (you run a program) you get a single thread (also known as the main thread) to run your application code. To explicitly start another thread (other than your application main thread) you have to create an instance of thread class and call its start method to run the thread using C#, Let's see an example

  using System;
  using System.Diagnostics;
  using System.Threading;

  public class Example
  {
     public static void Main()
     {
           //initialize a thread class object 
           //And pass your custom method name to the constructor parameter

           Thread thread = new Thread(SomeMethod);

           //start running your thread

           thread.Start();

           Console.WriteLine("Press Enter to terminate!");
           Console.ReadLine();
     }

     private static void SomeMethod()
     {
           //your code here that you want to run parallel
           //most of the cases it will be a CPU bound operation

           Console.WriteLine("Hello World!");
     }
  }

You can learn more in this tutorial Multithreading in C#, Here you will learn how to take advantage of Thread class and Task Parallel Library provided by C# and .NET Framework to create robust applications that are responsive, parallel and meet the user expectations.

user3114005
  • 31
  • 1
  • 4