Test foreground / worker behaviors : Background Thread « Thread « C# / CSharp Tutorial






using System;
using System.Threading;

class MainClass
{
  static void MyThreadProc()
  {
    Thread.CurrentThread.Name = "TheSecondaryThread";
    Thread secondaryThread = Thread.CurrentThread;
    Console.WriteLine("Name? {0}", secondaryThread.Name);
    Console.WriteLine("Alive? {0}", secondaryThread.IsAlive);
    Console.WriteLine("Priority? {0}", secondaryThread.Priority);      
    Console.WriteLine("State? {0}", secondaryThread.ThreadState);
    Console.WriteLine();

    for(int i = 0; i < 1000; i ++)
    {
      Console.WriteLine("Value of i is: {0}", i);
      Thread.Sleep(5);
    }
  }

  [MTAThread]
  static void Main(string[] args)
  {
    Thread secondaryThread = new Thread(new ThreadStart(MyThreadProc));
    secondaryThread.Priority = ThreadPriority.Highest;
    
    secondaryThread.IsBackground = true;
    secondaryThread.Start();
  }
}
Name? TheSecondaryThread
Alive? True
Priority? Highest
State? Background

Value of i is: 0








20.9.Background Thread
20.9.1.Move thread to background
20.9.2.Test foreground / worker behaviors
20.9.3.Set IsBackground to true