C#/WPF_예제소스

[C#] Thread vs Threadstart

딸기우유중독 2022. 2. 18. 17:57

결론부터

new Thread(SomeMethod).Start(); 쓸때 인자가 없으면 자동으로

new Thread(new ThreadStart(SomeMethod)); 이걸로 암묵적으로 캐스팅하고

인자값 있으면

new Thread(new ParameterizedThreadStart(SomeMethod)); 이걸로 암묵적으로 캐스팅함.

 

SomeMethod 가 파라미터가 있는 메소드냐 아니냐에 따라 호출하는게 다름.

 

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?

 

 


Asked 6 years, 10 months ago
Viewed 26k times
 
26
7

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?

Follow
pyrocumulus
8,5342 gold badges37 silver badges53 bronze badges
asked Apr 25, 2015 at 7:15
Shashank sarma
3631 gold badge3 silver badges8 bronze badges

3 Answers

42
 

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");

 

출처:https://stackoverflow.com/questions/29862234/thread-vs-threadstart

 

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 ThreadSt...

stackoverflow.com

 

728x90