์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- ์ฝ๋ฉ
- K๋ฐฐํฐ๋ฆฌ
- ๋ฐ์ํ
- ์ปดํจํฐ๊ณผํ
- ๊นํ๋จ
- Python
- ์๋ฐ
- ๋ ์
- ๋ผํ๋ผ์ค์๋ง๋
- JavaScript
- ๋ง์ผ๋ด๊ฐ์ธ์์๋ค์์ฐ๋ค๋ฉด
- Java
- database
- ComputerScience
- ์๋ฐ์คํฌ๋ฆฝํธ
- ์ํ
- ์ฑ
- ์ฅํธ์์ค
- css
- html
- ์นํผ๋ธ๋ฆฌ์ฑ
- ์นํ์ด์ง๋ง๋ค๊ธฐ
- ๊น๋ฏธ๊ฒฝ์๋งํ์์
- ๊น๋ฏธ๊ฒฝ
- ๊ฐ๋ฐ
- K๋ฐฐํฐ๋ฆฌ๋ ๋ณผ๋ฃจ์
- ํ๋ก๊ทธ๋๋ฐ
- ์ค๋ผํด
- ํ์ด์ฌ
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- Today
- Total
JiYoung Dev ๐ฅ
[C#] System.Threading.Timer ์ฐ๋ ๋ ๊ตฌํ ๋ณธ๋ฌธ
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
https://jeongkyun-it.tistory.com/90
'Question' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] ๋ฐฐ์ด ํฌ๊ธฐ ๋ณ๊ฒฝํ๊ธฐ (0) | 2023.08.17 |
---|---|
[JAVA] ๋ฌธ์์ด ์๋ฅด๊ธฐ(substring) (0) | 2023.08.11 |
[Thymeleaf] th:onclick๊ณผ location.href ํจ๊ป ์ฌ์ฉํ๊ธฐ (0) | 2023.08.08 |
[JAVA] String์์ int๋ก ๋ณํ (0) | 2023.08.08 |
[JAVA] ์ ๊ณฑ๊ทผ ๊ตฌํ๊ธฐ (Math) (0) | 2023.08.08 |