OpenReadCompletedEventHandler 委托
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示将用来处理 OpenReadCompleted 的 WebClient 事件的方法。
public delegate void OpenReadCompletedEventHandler(System::Object ^ sender, OpenReadCompletedEventArgs ^ e);
public delegate void OpenReadCompletedEventHandler(object sender, OpenReadCompletedEventArgs e);
type OpenReadCompletedEventHandler = delegate of obj * OpenReadCompletedEventArgs -> unit
Public Delegate Sub OpenReadCompletedEventHandler(sender As Object, e As OpenReadCompletedEventArgs)
参数
- sender
- Object
事件源。
一个包含事件数据的 OpenReadCompletedEventArgs。
示例
下面的代码示例演示如何下载用于读取的资源。
void OpenResourceForReading2( String^ address )
{
WebClient^ client = gcnew WebClient;
Uri ^uri = gcnew Uri(address);
client->OpenReadCompleted += gcnew OpenReadCompletedEventHandler( OpenReadCallback2 );
client->OpenReadAsync( uri );
}
public static void OpenResourceForReading2(string address)
{
WebClient client = new WebClient();
Uri uri = new Uri(address);
client.OpenReadCompleted += new OpenReadCompletedEventHandler(OpenReadCallback2);
client.OpenReadAsync(uri);
}
Public Shared Sub OpenResourceForReading2(ByVal address As String)
Dim client As WebClient = New WebClient()
AddHandler client.OpenReadCompleted, AddressOf OpenReadCallback2
Dim uri as Uri = New Uri(address)
client.OpenReadAsync(uri)
End Sub
下载完成后,将调用以下方法。
void OpenReadCallback2( Object^ /*sender*/, OpenReadCompletedEventArgs^ e )
{
Stream^ reply = nullptr;
StreamReader^ s = nullptr;
try
{
reply = dynamic_cast<Stream^>(e->Result);
s = gcnew StreamReader( reply );
Console::WriteLine( s->ReadToEnd() );
}
finally
{
if ( s != nullptr )
{
s->Close();
}
if ( reply != nullptr )
{
reply->Close();
}
}
}
private static void OpenReadCallback2(Object sender, OpenReadCompletedEventArgs e)
{
Stream reply = null;
StreamReader s = null;
try
{
reply = (Stream)e.Result;
s = new StreamReader(reply);
Console.WriteLine(s.ReadToEnd());
}
finally
{
if (s != null)
{
s.Close();
}
if (reply != null)
{
reply.Close();
}
}
}
Private Shared Sub OpenReadCallback2(ByVal sender As Object, ByVal e As OpenReadCompletedEventArgs)
Dim reply As Stream = Nothing
Dim s As StreamReader = Nothing
Try
reply = CType(e.Result, Stream)
s = New StreamReader(reply)
Console.WriteLine(s.ReadToEnd())
Finally
If Not s Is Nothing Then
s.Close()
End If
If Not reply Is Nothing Then
reply.Close()
End If
End Try
End Sub
注解
创建 OpenReadCompletedEventHandler 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。
扩展方法
GetMethodInfo(Delegate) |
获取指示指定委托表示的方法的对象。 |