2012-11-02 41 views
1

我想知道你是否可以通過讓用戶輸入一個字符串(最低,BelowNormal等)來改變線程的優先級?據我所知,「ThreadPriority」是一個枚舉,但我不知道該怎麼做。用console.readline設置線程優先級? c#

thread.Priority = ThreadPriority.BelowNormal 

如何將BelowNormal更改爲用戶輸入的內容(ReadLine)?

謝謝!

+0

是它發送一個數字那麼簡單對應於枚舉優先級的值? –

+0

試着用Enum.Parse解析它 – AntLaC

回答

3
thread.Priority = (ThreadPriority)Enum.Parse(typeof(ThreadPriority), Console.ReadLine()); 
+0

謝謝!我真的很感激快速回復! –

-2

你可以解析字符串,做一個條件

string userinput = Console.ReadLine(); 
if (userinput.Contains("BelowNormal")) 
{ 
    thread.Priority = ThreadPriority.BelowNormal; 
} 
+0

謝謝!我真的很感激快速回復! –

+0

Thx,自從我使用控制檯以來一直是永恆的 –

+0

那麼,如果我輸入'NotBelowNormal',該怎麼辦? –

1

您可以使用Enum.Parse,例如使用重載方法與ignoreCase

thread.Priority = (ThreadPriority)Enum.Parse(typeof(ThreadPriority), 
             "belownormal", true); 
+0

謝謝!我真的很感激快速回復! –