문제

I'm wondering if you can change the priority of a thread by letting the user enter a string (Lowest, BelowNormal etc)? As far as I know, the "ThreadPriority" is an enum but I don't know how to do it.

thread.Priority = ThreadPriority.BelowNormal

How can I change the BelowNormal to something the user enter (ReadLine)?

Thanks!

도움이 되었습니까?

해결책

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

다른 팁

You can use Enum.Parse, example uses overload method with ignoreCase:

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

You could parse the string and do a conditional

string userinput = Console.ReadLine();
if (userinput.Contains("BelowNormal"))
{
    thread.Priority = ThreadPriority.BelowNormal;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top