C#

C#에서 BackgroundWorker를 사용하기

saltdoll 2017. 10. 6. 08:22
반응형

DoWorkEventHndler를 이용해서, 

(DoWork이벤트를 처리할 메서드)

예제

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    // Do not access the form's BackgroundWorker reference directly.
    // Instead, use the reference provided by the sender parameter.
    BackgroundWorker bw = sender as BackgroundWorker;

    // Extract the argument.
    int arg = (int)e.Argument;

    // Start the time-consuming operation.
    e.Result = TimeConsumingOperation(bw, arg);

    // If the operation was canceled by the user, 
    // set the DoWorkEventArgs.Cancel property to true.
    if (bw.CancellationPending)
    {
        e.Cancel = true;
    } 

} 


Tool박스에 있는, BackroundWorker를 이용해서, 쓰레드를 만들 수도 있습니다.



[참고]

DoWorkEventHandler: https://msdn.microsoft.com/ko-kr/library/system.componentmodel.doworkeventhandler(v=vs.110).aspx

Youtube 동영상: https://youtu.be/G3zRhhGCJJA



반응형