๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[C#] System.Threading.Timer ์“ฐ๋ ˆ๋“œ ๊ตฌํ˜„ ๋ณธ๋ฌธ

Question

[C#] System.Threading.Timer ์“ฐ๋ ˆ๋“œ ๊ตฌํ˜„

Shinjio 2023. 8. 10. 12:00
System.Threading.Timer ํ™œ์šฉํ•˜์—ฌ Thread ๊ตฌํ˜„ํ•˜๊ธฐ

 

 

 

System.Threading.Timer 

์ง€์ •๋œ ๊ฐ„๊ฒฉ์œผ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ œ๊ณต

 

 

์‚ฌ์šฉ๋ฒ•

 

์„ ์–ธ

System.Threading.Timer thread;

 

 

๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ์“ฐ๋ ˆ๋“œ ์‹คํ–‰

๊ฐ์ฒด ์ƒ์„ฑ์‹œ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‹คํ–‰ํ•˜๊ฒŒ ๋  ๋ฉ”์„œ๋“œ๋ฅผ ์ฝœ๋ฐฑ ๋ฉ”์„œ๋“œ๋กœ ๋“ฑ๋กํ•ด์•ผ ํ•จ (ThreadRandNum)

 

thread.Change(num1, num2)์—์„œ

num1 ์€ ์‹œ์ž‘ํ•˜๊ธฐ ์ „ ๋Œ€๊ธฐ(์ง€์—ฐ) ์‹œ๊ฐ„,

num2๋Š” ๋ฐ˜๋ณต ์ฃผ๊ธฐ

 

thread = new System.Threading.Timer(ThreadRandNum);
thread.Change(0, 2000);

 

 

BeginInvoke ์‚ฌ์šฉ

์œˆ๋„์šฐ ์‘์šฉํ”„๋กœ๊ทธ๋žจ์˜ UI Thread์™€ ์—ฐ๊ด€๋  ๊ฒฝ์šฐ Cross Thread ๋ฌธ์ œ ๋ฐœ์ƒ 

์ด ๋•Œ Invoke ํ˜น์€ BeginInvoke ์‚ฌ์šฉ

BeginInvoke์˜ ๊ฒฝ์šฐ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰ํ•  ๋•Œ Invoke๋Š” ๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰ํ•  ๋•Œ ์‚ฌ์šฉ

 

 

TimerEventFiredDelegate() ์„ ์–ธ

delegate void TimerEventFiredDelegate();

 

 

Callback ํ•จ์ˆ˜ ๋‚ด์—์„œ delegate ์‚ฌ์šฉ

Callback ํ•จ์ˆ˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” object๊ฐ€ ํ•„์ˆ˜

TimerEventFiredDelgate(Work)์—๋Š” ์ˆ˜ํ–‰ํ•  ํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด์คŒ

 

private void ThreadRandNum(object obj)
{
  BeginInvoke(new TimerEventFiredDelegate(RandNum));
}

 

 

Work ํ•จ์ˆ˜ ์ •์˜

TimerEventFiredDelgate(Work)๊ฐ€ ์ˆ˜ํ–‰ํ•  ํ•จ์ˆ˜ 

 

private void RandNum()
{
    Random rand = new Random();
    randNum = rand.Next(1, 50);
    tbRandNum.Text = randNum.ToString();
    tbRandNum.Update();
}

 

 

์ „์ฒด ์ฝ”๋“œ 

 

using System.Security.Cryptography.Xml;
using System.Transactions;
using System.Windows.Forms.Design;

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        delegate void TimerEventFiredDelegate();

        System.Threading.Timer thread;
        System.Threading.Timer thread2;
        System.Threading.Timer thread3;
        System.Threading.Timer thread4;
        System.Threading.Timer thread5;

        public static int randNum = 0;
        public static int coutUpNum = 1;
        public static int countDownNum = 10;

        public Form1()
        {
            InitializeComponent();
        }


        private void btn_Click(object sender, EventArgs e)
        {
            thread = new System.Threading.Timer(ThreadRandNum);
            thread.Change(0, 2000);

            thread2 = new System.Threading.Timer(ThreadGuGu);
            thread2.Change(0, 2000);

            thread3 = new System.Threading.Timer(ThreadCountUp);
            thread3.Change(0, 2000);

            thread4 = new System.Threading.Timer(ThreadCountDown);
            thread4.Change(0, 2000);

            thread5 = new System.Threading.Timer(ThreadRandString);
            thread5.Change(0, 2000);

        }

        private void ThreadRandNum(object obj)
        {
            BeginInvoke(new TimerEventFiredDelegate(RandNum));
        }

        private void RandNum()
        {
            Random rand = new Random();
            randNum = rand.Next(1, 50);
            tbRandNum.Text = randNum.ToString();
            tbRandNum.Update();
        }

        private void ThreadGuGu(object obj)
        {
            BeginInvoke(new TimerEventFiredDelegate(RandGuGu));
        }

        private void RandGuGu()
        {
            int num1 = 0;
            int num2 = 0;

            Random randNum = new Random();
            num1 = randNum.Next(1, 9);
            num2 = randNum.Next(1, 9);

            tbMul.Text = num1 + "*" + num2 + "=" + (num1 * num2);
            tbMul.Update();
        }

        private void ThreadCountUp(object obj)
        {
            BeginInvoke(new TimerEventFiredDelegate(CountUp));
        }

        private void CountUp()
        {
            if(coutUpNum == 10)
            {
                return;
            }
            else
            {
                coutUpNum++;
                tbCountUp.Text = coutUpNum.ToString();
                tbCountUp.Update();
            }
        }

        private void ThreadCountDown(object obj)
        {
            BeginInvoke(new TimerEventFiredDelegate(CountDown));
        }

        private void CountDown()
        {
            if(countDownNum == 0)
            {
                return;
            }
            else
            {
                countDownNum--;
                tbCountDown.Text = countDownNum.ToString();
                tbCountDown.Update();
            }
        }

        private void ThreadRandString(object obj)
        {
            BeginInvoke(new TimerEventFiredDelegate(RandString));
        }

        private void RandString()
        {
            String[] text = { "์•ˆ๋…•", "hello", "bonjour", "hi", "good morning" };
            Random rand = new Random();
            int index = rand.Next(text.Length);
            tbPlus.Text = text[index];
            tbPlus.Update();
        }
    }
}

 

 

์‹คํ–‰ ๊ฒฐ๊ณผ

 

 

์ฐธ๊ณ ์ž๋ฃŒ

 

https://jeongkyun-it.tistory.com/59

 

C# ํฌ๋กœ์Šค ์Šค๋ ˆ๋“œ(Cross Thread)๋ž€? (์—๋Ÿฌ ๋ฐœ์ƒ ์ด์œ  / ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•)

์„œ๋ก  Winform์—์„œ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•ด์„œ ํผ์„ ์ œ์–ดํ•˜๋‹ค๋ณด๋ฉด ์•„๋ž˜์˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์€ ํฌ๋กœ์Šค ์Šค๋ ˆ๋“œ ์—๋Ÿฌ๊ฐ€ ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋ฐœ์ƒํ•˜๋Š”๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์—๋Ÿฌ ๋‚ด์šฉ : ํฌ๋กœ์Šค ์Šค๋ ˆ๋“œ ์ž‘์—…์ด ์ž˜๋ชป

jeongkyun-it.tistory.com

https://jeongkyun-it.tistory.com/90

 

C# Invoke์™€ BeginInvoke์˜ ์ฐจ์ด์  (๋ชฉ์  / ์ •์˜ / ์‚ฌ์šฉ ๋ฐฉ๋ฒ• / ์˜ˆ์ œ)

์„œ๋ก  ์ด๋ฒˆ ๊ธ€์€ Winform Application์—์„œ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ์ˆ ์ธ Invoke์™€ BeginInvoke์˜ ์ •์˜์™€ ์ฐจ์ด์ ์— ๋Œ€ํ•ด ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. ์šฐ์„  ํ•ด๋‹น ๊ธ€์„ ์ž‘์„ฑํ•˜๊ธฐ ์•ž์„œ ์Šค๋ ˆ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ UI๋ฅผ ์กฐ

jeongkyun-it.tistory.com