DownloadDataCompletedEventArgs 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为 DownloadDataCompleted 事件提供数据。
public ref class DownloadDataCompletedEventArgs : System::ComponentModel::AsyncCompletedEventArgs
public class DownloadDataCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs
type DownloadDataCompletedEventArgs = class
inherit AsyncCompletedEventArgs
Public Class DownloadDataCompletedEventArgs
Inherits AsyncCompletedEventArgs
- 继承
示例
下面的代码示例演示如何下载用户指定的资源。
// Sample call : DownLoadDataInBackground ("http://www.contoso.com/GameScores.html");
void DownloadDataInBackground( String^ address )
{
System::Threading::AutoResetEvent^ waiter = gcnew System::Threading::AutoResetEvent( false );
WebClient^ client = gcnew WebClient;
Uri ^uri = gcnew Uri(address);
// Specify that the DownloadDataCallback method gets called
// when the download completes.
client->DownloadDataCompleted += gcnew DownloadDataCompletedEventHandler( DownloadDataCallback );
client->DownloadDataAsync( uri, waiter );
// Block the main application thread. Real applications
// can perform other tasks while waiting for the download to complete.
waiter->WaitOne();
}
// Sample call : DownLoadDataInBackground ("http://www.contoso.com/GameScores.html");
public static void DownloadDataInBackground(string address)
{
System.Threading.AutoResetEvent waiter = new System.Threading.AutoResetEvent(false);
WebClient client = new WebClient();
Uri uri = new Uri(address);
// Specify that the DownloadDataCallback method gets called
// when the download completes.
client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(DownloadDataCallback);
client.DownloadDataAsync(uri, waiter);
// Block the main application thread. Real applications
// can perform other tasks while waiting for the download to complete.
waiter.WaitOne();
}
' Sample call : DownLoadDataInBackground ("http:' www.contoso.com/GameScores.html")
Public Shared Sub DownloadDataInBackground(ByVal address As String)
Dim waiter As System.Threading.AutoResetEvent = New System.Threading.AutoResetEvent(False)
Dim client As WebClient = New WebClient()
' Specify that the DownloadDataCallback method gets called
' when the download completes.
AddHandler client.DownloadDataCompleted, AddressOf DownloadDataCallback
Dim uri as Uri = New Uri(address)
client.DownloadDataAsync(uri, waiter)
' Block the main application thread. Real applications
' can perform other tasks while waiting for the download to complete.
waiter.WaitOne()
End Sub
下载完成后,将调用以下方法。
void DownloadDataCallback( Object^ /*sender*/, DownloadDataCompletedEventArgs^ e )
{
System::Threading::AutoResetEvent^ waiter = dynamic_cast<System::Threading::AutoResetEvent^>(e->UserState);
try
{
// If the request was not canceled and did not throw
// an exception, display the resource.
if ( !e->Cancelled && e->Error == nullptr )
{
array<Byte>^data = dynamic_cast<array<Byte>^>(e->Result);
String^ textData = System::Text::Encoding::UTF8->GetString( data );
Console::WriteLine( textData );
}
}
finally
{
// Let the main application thread resume.
waiter->Set();
}
}
private static void DownloadDataCallback(Object sender, DownloadDataCompletedEventArgs e)
{
System.Threading.AutoResetEvent waiter = (System.Threading.AutoResetEvent)e.UserState;
try
{
// If the request was not canceled and did not throw
// an exception, display the resource.
if (!e.Cancelled && e.Error == null)
{
byte[] data = (byte[])e.Result;
string textData = System.Text.Encoding.UTF8.GetString(data);
Console.WriteLine(textData);
}
}
finally
{
// Let the main application thread resume.
waiter.Set();
}
}
Private Shared Sub DownloadDataCallback(ByVal sender As Object, ByVal e As DownloadDataCompletedEventArgs)
Dim waiter As System.Threading.AutoResetEvent = CType(e.UserState, System.Threading.AutoResetEvent)
Try
' If the request was not canceled and did not throw
' an exception, display the resource.
If e.Cancelled = False AndAlso e.Error Is Nothing Then
Dim data() As Byte = CType(e.Result, Byte())
Dim textData As String = System.Text.Encoding.UTF8.GetString(data)
Console.WriteLine(textData)
End If
Finally
' Let the main application thread resume.
waiter.Set()
End Try
End Sub
注解
此类的实例将传递给 DownloadDataCompletedEventHandler。
属性
Cancelled |
获取一个值,该值指示异步操作是否已被取消。 (继承自 AsyncCompletedEventArgs) |
Error |
获取一个值,该值指示异步操作期间发生的错误。 (继承自 AsyncCompletedEventArgs) |
Result |
获取由 DownloadDataAsync 方法下载的数据。 |
UserState |
获取异步任务的唯一标识符。 (继承自 AsyncCompletedEventArgs) |
方法
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
RaiseExceptionIfNecessary() |
如果异步操作失败,则引发用户提供的异常。 (继承自 AsyncCompletedEventArgs) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |