C#

C# Thread (Process RealTime 및 우선순위)

sheepone 2021. 6. 29. 10:11
반응형

메인 쓰레드를 만들때 사용

(프로세스 우선순위 최대, 쓰레드 우선순위 최대, Sleep 최소화)

 

using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApp40
{
    public partial class Form1 : Form
    {
        Thread MainThread ;

        public Form1()
        {
            InitializeComponent();

            MainThread = new Thread(Update);

            //WinApi.TimeBeginPeriod(1);
            //Process 우선순위 RealTime
            Process.GetCurrentProcess().PriorityBoostEnabled = true;
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;

            //Thread 우선순위
            MainThread.Priority = ThreadPriority.Highest;

            MainThread.Start();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //종료전 쓰레드 중지
            MainThread.Abort();
            MainThread.Join();
        }

        public void Update()
        {
            Debug.WriteLine("Thread {0}: {1}, Priority {2}", Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.ThreadState, Thread.CurrentThread.Priority);
            while (true)
            {
                Thread.Sleep(0);
                //여기에 할일 작성
            }
        }
    }
}

 

반응형