์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- ์ฝ๋ฉ
- K๋ฐฐํฐ๋ฆฌ๋ ๋ณผ๋ฃจ์
- ์ํ์ฃผ
- ์ค๋ธ์
- database
- Python
- JavaScript
- ์ ๋ฆฌํธ๋ฆฌํธ
- ์ฑ
- ๋๊ฐ
- ํ์ด์ฌ
- css
- ๊ฐ์ดํ ์ข ๋ญ๊ฐ๋น
- ๋ ์
- ๋ฐฐ์์ ๋ฐฐ์
- ํ์ฒ์ ๋ฆฌํธ๋ฆฌํธ
- ํ๋ก๊ทธ๋๋ฐ
- Java
- ํฐ์คํ ๋ฆฌ์ฑ๋ฆฐ์ง
- ๋ฐ์ํ
- ์ค๋ผํด
- ์นดํ๋๊ฐ
- ์นํผ๋ธ๋ฆฌ์ฑ
- ์๋ฐ์คํฌ๋ฆฝํธ
- ๊น๋ฏธ๊ฒฝ์๋งํ์์
- ๊ฐ๋ฐ
- html
- ์ํ
- ์๋ฐ
- 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
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
'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 |