create a program that starts two new threads, and then 2 more threads inside those threads in csharp

To create a program that starts two new threads, and then two more threads inside those threads, you need to use the System.Threading namespace in C#.

main.cs
using System;
using System.Threading;

class Program
{
    static void Main()
    {
        // Create the first two threads
        Thread t1 = new Thread(() =>
        {
            Console.WriteLine("Thread 1 started");
            Thread t3 = new Thread(() =>
            {
                Console.WriteLine("Thread 3 started");
            });
            t3.Start();
        });

        Thread t2 = new Thread(() =>
        {
            Console.WriteLine("Thread 2 started");
            Thread t4 = new Thread(() =>
            {
                Console.WriteLine("Thread 4 started");
            });
            t4.Start();
        });

        // Start the first two threads
        t1.Start();
        t2.Start();

        Console.ReadLine();
    }
}
762 chars
36 lines

In this code, we create two threads t1 and t2. Each of these threads starts another thread inside them, t3 and t4. When the program is run, it will output the following:

main.cs
Thread 1 started
Thread 2 started
Thread 3 started
Thread 4 started
68 chars
5 lines

This shows that both the first and second threads started successfully, and that both the third and fourth threads were started inside those threads.

gistlibby LogSnag