다음을 통해 공유


UploadFileCompletedEventHandler 대리자

정의

UploadFileCompletedWebClient 이벤트를 처리할 메서드를 나타냅니다.

public delegate void UploadFileCompletedEventHandler(System::Object ^ sender, UploadFileCompletedEventArgs ^ e);
public delegate void UploadFileCompletedEventHandler(object sender, UploadFileCompletedEventArgs e);
type UploadFileCompletedEventHandler = delegate of obj * UploadFileCompletedEventArgs -> unit
Public Delegate Sub UploadFileCompletedEventHandler(sender As Object, e As UploadFileCompletedEventArgs)

매개 변수

sender
Object

이벤트 소스입니다.

e
UploadFileCompletedEventArgs

이벤트 데이터가 들어 있는 UploadFileCompletedEventArgs입니다.

예제

다음 코드 예제에서는 파일을 비동기적으로 업로드하는 방법을 보여 줍니다.

// Sample call: UploadFileInBackground("http://www.contoso.com/fileUpload.aspx", "data.txt")
void UploadFileInBackground( String^ address, String^ fileName )
{
   System::Threading::AutoResetEvent^ waiter = gcnew System::Threading::AutoResetEvent( false );
   WebClient^ client = gcnew WebClient;
   Uri ^uri = gcnew Uri(address);
   String^ method = "POST";

   // Specify that that UploadFileCallback method gets called
   // when the file upload completes.
   client->UploadFileCompleted += gcnew UploadFileCompletedEventHandler( UploadFileCallback );
   client->UploadFileAsync( uri, method, fileName, waiter );

   // Block the main application thread. Real applications
   // can perform other tasks while waiting for the upload to complete.
   waiter->WaitOne();
   Console::WriteLine( "File upload is complete." );
}
// Sample call: UploadFileInBackground("http://www.contoso.com/fileUpload.aspx", "data.txt")
public static void UploadFileInBackground(string address, string fileName)
{
    System.Threading.AutoResetEvent waiter = new System.Threading.AutoResetEvent(false);
    WebClient client = new WebClient();
    Uri uri = new Uri(address);
    string method = "POST";

    // Specify that that UploadFileCallback method gets called
    // when the file upload completes.
    client.UploadFileCompleted += new UploadFileCompletedEventHandler(UploadFileCallback);
    client.UploadFileAsync(uri, method, fileName, waiter);

    // Block the main application thread. Real applications
    // can perform other tasks while waiting for the upload to complete.
    waiter.WaitOne();
    Console.WriteLine("File upload is complete.");
}
'  Sample call: UploadFileInBackground("http:' www.contoso.com/fileUpload.aspx", "data.txt")
Public Shared Sub UploadFileInBackground(ByVal address As String, ByVal fileName As String)

    Dim waiter As System.Threading.AutoResetEvent = New System.Threading.AutoResetEvent(False)
    Dim client As WebClient = New WebClient()
    Dim method As String = "POST"
                 Dim uri as Uri =  New Uri(address)

    '  Specify that that UploadFileCallback method gets called
    '  when the file upload completes.
    AddHandler client.UploadFileCompleted, AddressOf UploadFileCallback
    client.UploadFileAsync(uri, method, fileName, waiter)

    '  Block the main application thread. Real applications
    '  can perform other tasks while waiting for the upload to complete.
    waiter.WaitOne()
    Console.WriteLine("File upload is complete.")
End Sub

다음 메서드는 업로드가 완료 되 면 호출 됩니다.

void UploadFileCallback( Object^ /*sender*/, UploadFileCompletedEventArgs^ e )
{
   System::Threading::AutoResetEvent^ waiter = dynamic_cast<System::Threading::AutoResetEvent^>(e->UserState);
   ;
   try
   {
      String^ reply = System::Text::Encoding::UTF8->GetString( e->Result );
      Console::WriteLine( reply );
   }
   finally
   {

      // If this thread throws an exception, make sure that
      // you let the main application thread resume.
      waiter->Set();
   }

}
private static void UploadFileCallback(Object sender, UploadFileCompletedEventArgs e)
{
    System.Threading.AutoResetEvent waiter = (System.Threading.AutoResetEvent)e.UserState; ;
    try
    {
        string reply = System.Text.Encoding.UTF8.GetString(e.Result);

        Console.WriteLine(reply);
    }
    finally
    {
        // If this thread throws an exception, make sure that
        // you let the main application thread resume.
        waiter.Set();
    }
}
Private Shared Sub UploadFileCallback(ByVal sender As Object, ByVal e As System.Net.UploadFileCompletedEventArgs)

    Dim waiter As System.Threading.AutoResetEvent = CType(e.UserState, System.Threading.AutoResetEvent)
    Try

        Dim reply As String = System.Text.Encoding.UTF8.GetString(e.Result)
        Console.WriteLine(reply)
    Finally
        '  If this thread throws an exception, make sure that
        '  you let the main application thread resume.
        waiter.Set()
    End Try
End Sub

설명

UploadFileCompletedEventHandler 대리자를 만들 때, 이벤트를 처리할 메서드를 식별합니다. 이벤트를 이벤트 처리기와 연결하려면 대리자의 인스턴스를 해당 이벤트에 추가합니다. 대리자를 제거하지 않는 경우 이벤트가 발생할 때마다 이벤트 처리기가 호출됩니다. 이벤트 처리기 대리자에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.

확장 메서드

GetMethodInfo(Delegate)

지정된 대리자가 나타내는 메서드를 나타내는 개체를 가져옵니다.

적용 대상