Thread Start() method in C#
In C#, the Start() method is used to begin execution of a new thread. The method is called on a Thread object, which represents the thread to be created and started.
How to use the Thread Start() method in C#
The Start() method takes no arguments and returns immediately, allowing the calling thread to continue execution while the new thread runs in parallel. Once the new thread has started, it will execute the method specified by the ThreadStart delegate that was passed to its constructor.
In the above example, the Main() method is the entry point for the program. It starts by outputting a message to the console indicating that the main thread is starting.
Next, a new Thread object is created, and its constructor is passed a reference to the ThreadWork() method as a ThreadStart delegate . This specifies that the new thread should start executing the ThreadWork() method.
After the new thread is created, the Start() method is called on it to begin execution. The Main() method then waits for the new thread to complete by calling its Join() method.
The ThreadWork() method simulates some work by executing a loop with a few sub-operations. Each sub-operation is output to the console, and a 1-second delay is added between each sub-operation using the Thread.Sleep() method. Finally, when the loop completes, a message is output to the console indicating that the new thread has completed.
When the program is run, the output will show that the messages from the Main() method and the ThreadWork() method are interleaved, indicating that they are executing in parallel:
Above example demonstrates how to use the Start() method in C# to create and start a new thread, allowing the main thread to continue executing while the new thread runs in parallel.