Creating and Starting Multiple Threads

In our first example, we'll create two new threads and use them to display text in a console application. One thread will type "No worries." 20 times, and the other will type "No Problems." 20 times. We start this new example, ch15_01.cs, by creating an object and calling a method named StartThreads from Main because we can't use only static methods in this example:

static void Main()
{
  ch15_01 app = new ch15_01();

  app.StartThreads();
}

In the StartThreads method we'll create two new threads, thread1 and thread2, using the Thread class's constructor. Threads operate on the method level in C#, which means you pass the name of the method that holds a thread's code using a ThreadStart delegate to the Thread ...

Get Microsoft® Visual C#® .NET 2003 Kick Start 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.