Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread vs Threadstart

In C#, practically, I haven't observed any difference between the following:

new Thread(SomeMethod).Start();

,

new Thread(new ParameterizedThreadStart(SomeMethod));

and

new Thread(new ThreadStart(SomeMethod));

What is the difference, if there is any at all?

like image 671
Shashank sarma Avatar asked Apr 25 '15 07:04

Shashank sarma


People also ask

What is difference between thread and ThreadStart in c#?

The difference between new Thread(SomeMethod) and new Thread(new ThreadStart(SomeMethod)) is purely syntactical: The C# compiler generates the same code for these; the former version is an abbreviation of the latter.

When to use ThreadStart in c#?

Thread(ThreadStart) Constructor is used to initialize a new instance of a Thread class. This constructor will give ArgumentNullException if the value of the parameter is null.

Is task a thread in C#?

Here are some differences between a task and a thread. The Thread class is used for creating and manipulating a thread in Windows. A Task represents some asynchronous operation and is part of the Task Parallel Library, a set of APIs for running tasks asynchronously and in parallel.


3 Answers

The Thread(ThreadStart) constructor can only be used when the signature of your SomeMethod method matches the ThreadStart delegate. Conversely, Thread(ParameterizedThreadStart) requires SomeMethod to match the ParameterizedThreadStart delegate. The signatures are below:

public delegate void ThreadStart()
public delegate void ParameterizedThreadStart(Object obj)

Concretely, this means that you should use ThreadStart when your method does not take any parameters, and ParameterizedThreadStart when it takes a single Object parameter. Threads created with the former should be started by calling Start(), whilst threads created with the latter should have their argument specified through Start(Object).

public static void Main(string[] args)
{
    var threadA = new Thread(new ThreadStart(ExecuteA));
    threadA.Start();

    var threadB = new Thread(new ParameterizedThreadStart(ExecuteB));
    threadB.Start("abc");

    threadA.Join();
    threadB.Join();
}

private static void ExecuteA()
{
    Console.WriteLine("Executing parameterless thread!");
}

private static void ExecuteB(Object obj)
{
    Console.WriteLine($"Executing thread with parameter \"{obj}\"!");
}

Finally, you can call the Thread constructors without specifying the ThreadStart or ParameterizedThreadStart delegate. In this case, the compiler will match your method to the constructor overload based on its signature, performing the cast implicitly.

var threadA = new Thread(ExecuteA);   // implicit cast to ThreadStart
threadA.Start();

var threadB = new Thread(ExecuteB);   // implicit cast to ParameterizedThreadStart
threadB.Start("abc");
like image 173
Douglas Avatar answered Oct 02 '22 03:10

Douglas


new Thread(SomeMethod) and new Thread(new ThreadStart(SomeMethod)):

The difference between new Thread(SomeMethod) and new Thread(new ThreadStart(SomeMethod)) is purely syntactical: The C# compiler generates the same code for these; the former version is an abbreviation of the latter.

(The compiler can automatically deduce the proper delegate type to use from the signatures of the available Thread constructors, and the signature of the specified method SomeMethod. Writing out new ThreadStart(…) instead of just is a little bit like replacing var with the actual type of some expression, you're sparing the compiler the work of figuring out the actual type.)

These two versions work when SomeMethod takes no parameters, because that's the signature required to match the ThreadStart delegate.

new Thread(new ParameterizedThreadStart(SomeMethod)):

The difference between the above two and new Thread(new ParameterizedThreadStart(SomeMethod)) is that this one is calling a different constructor on Thread.

And that ParameterizedThreadStart proscribes a different method signature than ThreadStart: Your SomeMethod needs to take one argument of type object, otherwise it does not match this delegate type.

like image 24
stakx - no longer contributing Avatar answered Oct 02 '22 04:10

stakx - no longer contributing


No, but there are a lot of times when your code look a lot nicer if you create the ThreadStart object in one place and starts the new thread in another place.

like image 21
idstam Avatar answered Oct 02 '22 03:10

idstam