WebClient.OpenReadCompleted 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當異步操作開啟包含資源的數據流完成時發生。
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
事件類型
範例
下列程式代碼範例示範如何設定此事件的事件處理程式。
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
備註
謹慎
WebRequest
、HttpWebRequest
、ServicePoint
和 WebClient
已經過時,您不應該將它們用於新的開發。 請改用 HttpClient。
每次異步操作開啟包含資源之數據流時,都會引發此事件。 這些作業會藉由呼叫 OpenReadAsync 方法啟動。
OpenReadCompletedEventHandler 是這個事件的委派。 OpenReadCompletedEventArgs 類別會提供事件處理程式與事件數據。
如需如何處理事件的詳細資訊,請參閱 處理和引發事件。