다음을 통해 공유


방법: 이벤트 기반 비동기 패턴을 지원하는 구성 요소 사용

업데이트: 2007년 11월

많은 구성 요소들은 작업을 비동기식으로 수행하는 옵션을 제공합니다. 예를 들어, SoundPlayerPictureBox 구성 요소를 사용하면 주 스레드가 중단 없이 계속 실행되는 동안 소리 및 이미지를 "백그라운드로" 로드할 수 있습니다.

이벤트 기반 비동기 패턴 개요를 지원하는 클래스에 비동기 메서드를 사용하는 작업은 다른 이벤트의 경우에서와 같이 구성 요소의 MethodNameCompleted 이벤트에 이벤트 처리기를 연결하는 것처럼 간단히 수행할 수 있습니다. MethodNameAsync 메서드를 호출하면 응용 프로그램은 MethodNameCompleted 이벤트가 발생될 때까지 중단 없이 계속 실행됩니다. 이벤트 처리기에서 AsyncCompletedEventArgs 매개 변수를 검토하여 비동기 작업이 완료되었는지 아니면 취소되었는지 확인할 수 있습니다.

이벤트 처리기를 사용하는 방법에 대한 자세한 내용은 이벤트 처리기 개요(Windows Forms)를 참조하십시오.

다음 절차에서는 PictureBox 컨트롤의 비동기 이미지 로드 기능을 사용하는 방법을 보여 줍니다.

PictureBox 컨트롤을 사용하여 이미지를 비동기식으로 로드하려면

  1. 폼에 PictureBox 구성 요소의 인스턴스를 만듭니다.

  2. LoadCompleted 이벤트에 이벤트 처리기를 할당합니다.

    여기에서 비동기 다운로드 중에 발생했을 수 있는 오류를 확인합니다. 작업의 취소 여부도 확인할 수 있습니다.

    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    
    public Form1()
    {
        InitializeComponent();
    
        this.pictureBox1.LoadCompleted += 
            new System.ComponentModel.AsyncCompletedEventHandler(this.pictureBox1_LoadCompleted);
    }
    
    Private Sub PictureBox1_LoadCompleted( _
        ByVal sender As System.Object, _
        ByVal e As System.ComponentModel.AsyncCompletedEventArgs) _
        Handles PictureBox1.LoadCompleted
    
        If (e.Error IsNot Nothing) Then
            MessageBox.Show(e.Error.Message, "Load Error")
        ElseIf e.Cancelled Then
            MessageBox.Show("Load cancelled", "Canceled")
        Else
            MessageBox.Show("Load completed", "Completed")
        End If
    
    End Sub
    
    private void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            MessageBox.Show(e.Error.Message, "Load Error");
        }
        else if (e.Cancelled)
        {
            MessageBox.Show("Load canceled", "Canceled");
        }
        else
        {
            MessageBox.Show("Load completed", "Completed");
        }
    }
    
  3. 폼에 loadButton 및 cancelLoadButton의 두 단추를 추가합니다. Click 이벤트를 추가하여 다운로드를 시작 및 취소합니다.

    Private Sub loadButton_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles loadButton.Click
    
        ' Replace with a real url.
        PictureBox1.LoadAsync("http://www.tailspintoys.com/image.jpg")
    
    End Sub
    
    private void loadButton_Click(object sender, EventArgs e)
    {
        // Replace with a real url.
        pictureBox1.LoadAsync("http://www.tailspintoys.com/image.jpg");
    }
    
    Private Sub cancelLoadButton_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles cancelLoadButton.Click
    
        PictureBox1.CancelAsync()
    
    End Sub
    
    private void cancelLoadButton_Click(object sender, EventArgs e)
    {
        pictureBox1.CancelAsync();
    }
    
  4. 응용 프로그램을 실행합니다.

    이미지 다운로드가 진행될 때 폼을 자유롭게 이동하고 최소화하고 최대화할 수 있습니다.

참고 항목

작업

방법: 백그라운드에서 작업 실행

개념

이벤트 기반 비동기 패턴 개요

기타 리소스

Visual Basic의 다중 스레딩