Creating New Threads

The act of creating and invoking a new thread in C# is relatively straightforward. The process involves creating a Thread object, instantiating a ThreadStart delegate with a delegate method handler, and passing the ThreadStart delegate to the new Thread object. All that's remaining is to start the thread, and off it runs. Listing 23.1 shows how to create and execute a new thread.

Listing 23.1. Creating a New Thread: SingleThread.cs
 using System; using System.Threading; /// <summary> /// Shows how to create a single thread of execution. /// </summary> class SingleThread { static void Main(string[] args) { SingleThread st = new SingleThread(); Thread th = new Thread(new ThreadStart(st.SayHello)); th.Start(); } public void ...

Get C# Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.