Csharp/CSharp Tutorial/Thread/Main Thread

Материал из .Net Framework эксперт
Версия от 15:20, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Display main thread"s name

<source lang="csharp">using System; using System.Threading;

class MainClass {

 public static void Main() { 
   Thread thrd = Thread.CurrentThread; 

   if(thrd.Name == null) 
     Console.WriteLine("Main thread has no name."); 
   else 
     Console.WriteLine("Main thread is called: " + thrd.Name); 

 } 

}</source>

Main thread has no name.

Display main thread"s priority

<source lang="csharp">using System; using System.Threading;

class MainClass {

 public static void Main() { 
   Thread thrd = Thread.CurrentThread; 

   Console.WriteLine("Priority: " + thrd.Priority); 

 } 

}</source>

Priority: Normal

Get the main thread

<source lang="csharp">using System; using System.Threading;

class MainClass {

 public static void Main() { 
   Thread thrd; 

   // Get the main thread. 
   thrd = Thread.CurrentThread; 


 } 

}</source>

Set main thread"s name

<source lang="csharp">using System; using System.Threading;

class MainClass {

 public static void Main() { 
   Thread thrd = Thread.CurrentThread; 

   thrd.Name = "Main Thread"; 

   Console.WriteLine("Main thread is now called: " + thrd.Name); 
 } 

}</source>

Main thread is now called: Main Thread

Set the main thread"s priority

<source lang="csharp">using System; using System.Threading;

class MainClass {

 public static void Main() { 
   Thread thrd = Thread.CurrentThread; 

   thrd.Priority = ThreadPriority.AboveNormal; 

   Console.WriteLine("Priority is now: " + thrd.Priority); 

 } 

}</source>

Priority is now: AboveNormal