Background threads

We can also declare a thread as background thread by specifying IsBackground property to true

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ThreadPrograms
{
    

    class Program
    {
        static void Main(string[] args)
        {
            var t1 = new Thread(ThreadMain) { Name = "MyNewThread", IsBackground = true};
            t1.Start();
            Console.WriteLine("Main thread ending now.");
        }

        static void ThreadMain()
        {
            Console.WriteLine("Thread {0} started", Thread.CurrentThread.Name);
            Thread.Sleep(3000);
            Console.WriteLine("Thread {0} completed", Thread.CurrentThread.Name);
        }
    }
}

 

 

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *