프로그래밍/.NetFramework
BackgroundWorker, ProgressBar 활용
쇠주는참이슬
2012. 9. 27. 16:44
출처 : http://weezzle.net/3327
BackgroundWorker(백그라운드워커), ProgressBar 활용
[BackgroundWorker]
BackgroundWorker는 별도의 쓰레드에게 일을 시키기 위해 사용하는 클래스.
[ProgressBar]
ProgressBar는 작업진행이 얼마 되었는지 보여주는 표시바이다.
public frmAutoGetList()
{
InitializeComponent();
bworker.WorkerReportsProgress = true;
bworker.WorkerSupportsCancellation = true;
bworker.DoWork += new DoWorkEventHandler(bworker_DoWork);
bworker.ProgressChanged += new ProgressChangedEventHandler(bworker_ProgressChanged);
bworker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bworker_RunWorkerCompleted);
}
//실행버튼
private void cmdEx_Click(object sender, EventArgs e)
{
bworker.RunWorkerAsync();
}
//취소버튼
private void cmdCancel_Click(object sender, EventArgs e)
{
bworker.CancelAsync();
this.Close();
}
//백그라운드 실제 처리할 작업
private void bworker_DoWork(object sender, DoWorkEventArgs e)
{
//백그라운드로 처리 할 코드 기입
//bworker.ReportProgress(숫자);
}
//작업 진행률 표시
private void bworker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pBar.Value = e.ProgressPercentage;
}
//작업이 완료
private void bworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message, "Error");
return;
}
}