Can someone please break down a simple statement for a relative newbie?

Thread firstThread = new Thread (new ThreadStart (Method1));

In other words, what is happening at each stage here:

new ThreadStart (Method1)
new Thread (new ThreadStart (Method1))
Thread firstThread = new Thread (new ThreadStart (Method1));

Thanks for any help you can give.

Recommended Answers

All 6 Replies

Thread is a built in class in .NET to allow threading.
firstThread is a new thread you are creating.

new Thread (new ThreadStart (Method1))

Creates a new instance of the thread.The code that is to be executed is method 'Method1'. The new thread cannot directly run this method so we have to create a delegate.

new ThreadStart (Method1)

ThreadStart is that delegate, we create a new instance of it and supply it to the thread.
After this you have to call Start to run the thread.

firstThread.Start();

You might want to check out this.

The new ThreadStart(Method1) simply encapsulates a method that will be called when the thread is started. The new Thread(new ThreadStart(Method1)) creates a new Thread object that can be used to reference the thread and start running the thread, with the starting point Method1. Hope this helps.

read the attached.

commented: I said before: has distinct way to provide help +8
commented: wow! nice +4

Thanks a lot, guys.

Thread t = new Thread(new ThreadStart(this.YourFunction));
t.Start();

Above code will start the execution of function YourFunction(). You can consider that function as the entry point of a new process. In the ThreadStart this.YourFunction is a delegate that has a pointer to the function to execute.

commented: since when did C# have functions? I thought they were methods for some reason.... -1

Somethings not mention, and should be made clear.

ThreadStart is a delegate. Delegates are used as an "alias" which points to some other function. They can be re-used for many functions not just the one.

The ThreadStart in particular is a NO ARGUMENT delegate and so when you create the referring function "Method1" it should not contain parameters.

The other delegate you can use is the ParameterizedThreadStart delegate which accepts only ONE ARGUMENT of type object. So when you create the referring function "MethodOneParam" it should contain 1 parameter of type object.

You cannot change the delegate signatures since these two are part of the Framework. But.. you most certainly can create your OWN delegates of multivariable type safe delegates.

Another important note is that using the Thread class in a WinForms/WPF application, you cannot refer to the underlying controls unless that Thread performs a synchronized postback through the SychronizationContext/Dispatcher as UI elements cannot be manipulated or referenced in other threads besides the UI's.

commented: excellent first post +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.