WebClient.OpenReadCompleted Event
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Occurs when an asynchronous operation to open a stream containing a resource completes.
public:
event System::Net::OpenReadCompletedEventHandler ^ OpenReadCompleted;
public event System.Net.OpenReadCompletedEventHandler? OpenReadCompleted;
public event System.Net.OpenReadCompletedEventHandler OpenReadCompleted;
member this.OpenReadCompleted : System.Net.OpenReadCompletedEventHandler
Public Custom Event OpenReadCompleted As OpenReadCompletedEventHandler
Public Event OpenReadCompleted As OpenReadCompletedEventHandler
The following code example demonstrates setting an event handler for this event.
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
The following code example shows an implementation of a handler for this event.
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
Caution
WebRequest
, HttpWebRequest
, ServicePoint
, and WebClient
are obsolete, and you shouldn't use them for new development. Use HttpClient instead.
This event is raised each time an asynchronous operation to open a stream containing a resource completes. These operations are started by calling the OpenReadAsync methods.
The OpenReadCompletedEventHandler is the delegate for this event. The OpenReadCompletedEventArgs class provides the event handler with event data.
For more information about how to handle events, see Handling and Raising Events.
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: