728x90
1. 강의 개요
이번 강의에서는 Timer 컨트롤을 사용하여 일정 간격으로 반복 작업을 수행하는 방법을 학습합니다.
Timer는 특정 시간 간격마다 이벤트를 발생시켜 작업을 주기적으로 실행할 수 있도록 도와줍니다.
2. 학습 목표
- Timer 컨트롤을 사용하여 작업을 주기적으로 실행.
- 타이머 시작/정지 기능 구현.
- Label, ProgressBar 등 UI 요소와 연동해 반복 작업 구현.
3. Timer 컨트롤이란?
Timer는 Windows Forms에서 일정 간격으로 작업을 수행하기 위한 컨트롤입니다.
- Interval 속성을 사용해 작업 간격(밀리초 단위) 설정.
- Tick 이벤트가 지정된 간격마다 호출.
Timer 주요 속성 및 메서드
속성/메서드 설명 예제
Interval | 타이머 간격(밀리초 단위) | timer1.Interval = 1000; |
Start() | 타이머 시작 | timer1.Start(); |
Stop() | 타이머 정지 | timer1.Stop(); |
Timer 주요 이벤트
이벤트 설명 예제
Tick | Interval 간격마다 호출 | timer1.Tick += Timer1_Tick; |
4. 실습: Timer를 사용한 반복 작업 구현
요구사항
- Timer를 사용하여 매 1초마다 ProgressBar 증가.
- Label에 현재 시간을 표시.
- 버튼으로 Timer 시작/정지 제어.
폼 구성
컨트롤 타입 이름 텍스트 위치 크기
Label | lblTime | "현재 시간: 00:00:00" | 상단 | (400 x 30) |
ProgressBar | progressBar1 | (없음) | 중간 | (400 x 30) |
Button | btnStart | "시작" | 하단 왼쪽 | (100 x 30) |
Button | btnStop | "정지" | 하단 오른쪽 | (100 x 30) |
코드 작성
Form1.cs
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private Timer timer1;
private int progressValue = 0;
public Form1()
{
InitializeComponent();
InitializeTimer();
}
private void InitializeTimer()
{
// Timer 설정
timer1 = new Timer
{
Interval = 1000 // 1초 간격
};
timer1.Tick += Timer1_Tick;
// 초기 상태
progressBar1.Value = 0;
lblTime.Text = $"현재 시간: {DateTime.Now:HH:mm:ss}";
}
// Timer Tick 이벤트 핸들러
private void Timer1_Tick(object sender, EventArgs e)
{
// 현재 시간 업데이트
lblTime.Text = $"현재 시간: {DateTime.Now:HH:mm:ss}";
// ProgressBar 증가
progressValue = (progressValue + 10) % 110; // 100을 넘으면 0으로 초기화
progressBar1.Value = progressValue;
}
// "시작" 버튼 클릭 이벤트
private void BtnStart_Click(object sender, EventArgs e)
{
timer1.Start();
btnStart.Enabled = false;
btnStop.Enabled = true;
}
// "정지" 버튼 클릭 이벤트
private void BtnStop_Click(object sender, EventArgs e)
{
timer1.Stop();
btnStart.Enabled = true;
btnStop.Enabled = false;
}
}
}
Form1.Designer.cs
namespace WindowsFormsApp1
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
private Label lblTime;
private ProgressBar progressBar1;
private Button btnStart;
private Button btnStop;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.lblTime = new Label();
this.progressBar1 = new ProgressBar();
this.btnStart = new Button();
this.btnStop = new Button();
this.SuspendLayout();
// lblTime
this.lblTime.AutoSize = true;
this.lblTime.Location = new System.Drawing.Point(20, 20);
this.lblTime.Name = "lblTime";
this.lblTime.Size = new System.Drawing.Size(150, 20);
this.lblTime.TabIndex = 0;
this.lblTime.Text = "현재 시간: 00:00:00";
// progressBar1
this.progressBar1.Location = new System.Drawing.Point(20, 60);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(400, 30);
this.progressBar1.TabIndex = 1;
// btnStart
this.btnStart.Location = new System.Drawing.Point(20, 110);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(100, 30);
this.btnStart.TabIndex = 2;
this.btnStart.Text = "시작";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.BtnStart_Click);
// btnStop
this.btnStop.Location = new System.Drawing.Point(140, 110);
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(100, 30);
this.btnStop.TabIndex = 3;
this.btnStop.Text = "정지";
this.btnStop.UseVisualStyleBackColor = true;
this.btnStop.Click += new System.EventHandler(this.BtnStop_Click);
// Form1
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(450, 180);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.lblTime);
this.Name = "Form1";
this.Text = "Timer 컨트롤 예제";
this.ResumeLayout(false);
this.PerformLayout();
}
}
}
5. 실행 결과
- 타이머 시작
- "시작" 버튼 클릭 시 ProgressBar가 1초 간격으로 증가합니다.
- Label에 현재 시간이 1초마다 업데이트됩니다.
- 타이머 정지
- "정지" 버튼 클릭 시 ProgressBar와 Label 업데이트가 멈춥니다.
- UI 상태 전환
- "시작" 버튼 클릭 시 "정지" 버튼 활성화, "정지" 버튼 클릭 시 "시작" 버튼 활성화.
6. 주요 개념 요약
- Timer는 주기적으로 작업을 실행하기 위한 컨트롤로, Tick 이벤트를 통해 반복 작업 처리.
- Interval 속성을 통해 작업 간격을 밀리초 단위로 설정 가능.
- 타이머의 시작과 정지는 Start() 및 Stop() 메서드를 사용.
728x90
'Study > C#' 카테고리의 다른 글
C# Windows Forms 강의 30편: DataGridView 컨트롤로 데이터 관리 (0) | 2025.03.05 |
---|---|
C# Windows Forms 강의 29편: OpenFileDialog와 SaveFileDialog로 파일 관리 (0) | 2025.03.04 |
C# Windows Forms 강의 27편: Chart 컨트롤을 사용한 데이터 시각화 (0) | 2025.03.02 |
C# Windows Forms 강의 26편: BackgroundWorker로 비동기 작업 처리 (0) | 2025.03.01 |
C# Windows Forms 강의 25편: 크로스 스레드 작업과 InvokeRequired (0) | 2025.02.28 |