Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,I am learning threading concepts and I could not make out the difference b/w Thread and ThreadStart. Below is my code....

C#
class Alpha
    {
        public void Beta()
        {
            for (int i = 0; i < 5;i++ )
            {
                Console.WriteLine("Alpha.Beta is running its own thread.......");
            }

        }
    }


C#
public class Simple
    {
        public static int Main()
        {
            Console.WriteLine("Thread Start/Stop/Join Sample");

            Alpha oAlpha = new Alpha();

// Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));
Thread oThread = new Thread(oAlpha.Beta);

oThread.Start();
while (!oThread.IsAlive);
Thread.Sleep(1);
C#
oThread.Join();
          Console.WriteLine();
          Console.WriteLine("Alpha.Beta has finished");

C#
Console.ReadKey();
           return 0;
       }


both of the underlined statements are giving the same result... can some explain the difference between "new Thread(something) and new Thread(new ThreadStart(something))"
Posted

1 solution

See the documentation: MSDN ThreadStart Delegate[^]
It says:
"Visual Basic and C# users can omit the ThreadStart or ParameterizedThreadStart delegate constructor when creating a thread. In Visual Basic, use the AddressOf operator when passing your method to the Thread constructor; for example, Dim t As New Thread(AddressOf ThreadProc). In C#, simply specify the name of the thread procedure. The compiler selects the correct delegate constructor."


Basically, you don't need to use the ThreadStart at all as the compiler sorts it out for you and uses the appropriate delegate automatically.
 
Share this answer
 
Comments
CPallini 21-Jul-15 3:55am    
5.

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