PingCompletedEventArgs Class
Provides data for the PingCompleted event.
System::EventArgs
System.ComponentModel::AsyncCompletedEventArgs
System.Net.NetworkInformation::PingCompletedEventArgs
Assembly: System (in System.dll)
The PingCompletedEventArgs type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | Cancelled | Gets a value indicating whether an asynchronous operation has been canceled. (Inherited from AsyncCompletedEventArgs.) |
![]() | Error | Gets a value indicating which error occurred during an asynchronous operation. (Inherited from AsyncCompletedEventArgs.) |
![]() | Reply | Gets an object that contains data that describes an attempt to send an Internet Control Message Protocol (ICMP) echo request message and receive a corresponding ICMP echo reply message. |
![]() | UserState | Gets the unique identifier for the asynchronous task. (Inherited from AsyncCompletedEventArgs.) |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | RaiseExceptionIfNecessary | Raises a user-supplied exception if an asynchronous operation failed. (Inherited from AsyncCompletedEventArgs.) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
Instances of this class are passed to a PingCompletedEventHandler method that is called when a SendAsync call completes. The SendAsync methods send an Internet Control Message Protocol (ICMP) echo request asynchronously and wait for a corresponding ICMP echo reply message. The Reply property contains the results of the ICMP echo request.
The following code example demonstrates sending an ICMP echo request asynchronously.
#using <System.dll> using namespace System; using namespace System::Text; using namespace System::Net; using namespace System::Net::NetworkInformation; using namespace System::ComponentModel; using namespace System::Threading; void PingCompletedCallback( Object^ sender, PingCompletedEventArgs^ e ); void DisplayReply( PingReply^ reply ); int main() { array<String^>^args = Environment::GetCommandLineArgs(); if ( args->Length == 1 ) throw gcnew ArgumentException( "Ping needs a host or IP Address." ); String^ who = args[ 1 ]; AutoResetEvent^ waiter = gcnew AutoResetEvent( false ); Ping ^ pingSender = gcnew Ping; // When the PingCompleted event is raised, // the PingCompletedCallback method is called. pingSender->PingCompleted += gcnew PingCompletedEventHandler( PingCompletedCallback ); // Create a buffer of 32 bytes of data to be transmitted. String^ data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; array<Byte>^buffer = Encoding::ASCII->GetBytes( data ); // Wait 12 seconds for a reply. int timeout = 12000; // Set options for transmission: // The data can go through 64 gateways or routers // before it is destroyed, and the data packet // cannot be fragmented. PingOptions ^ options = gcnew PingOptions( 64,true ); Console::WriteLine( "Time to live: {0}", options->Ttl ); Console::WriteLine( "Don't fragment: {0}", options->DontFragment ); // Send the ping asynchronously. // Use the waiter as the user token. // When the callback completes, it can wake up this thread. pingSender->SendAsync( who, timeout, buffer, options, waiter ); // Prevent this example application from ending. // A real application should do something useful // when possible. waiter->WaitOne(); Console::WriteLine( "Ping example completed." ); } void PingCompletedCallback( Object^ /*sender*/, PingCompletedEventArgs^ e ) { // If the operation was canceled, display a message to the user. if ( e->Cancelled ) { Console::WriteLine( "Ping canceled." ); // Let the main thread resume. // UserToken is the AutoResetEvent object that the main thread // is waiting for. (dynamic_cast<AutoResetEvent^>(e->UserState))->Set(); } // If an error occurred, display the exception to the user. if ( e->Error != nullptr ) { Console::WriteLine( "Ping failed:" ); Console::WriteLine( e->Error->ToString() ); // Let the main thread resume. (dynamic_cast<AutoResetEvent^>(e->UserState))->Set(); } PingReply ^ reply = e->Reply; DisplayReply( reply ); // Let the main thread resume. (dynamic_cast<AutoResetEvent^>(e->UserState))->Set(); } void DisplayReply( PingReply ^ reply ) { if ( reply == nullptr ) return; Console::WriteLine( "ping status: {0}", reply->Status ); if ( reply->Status == IPStatus::Success ) { Console::WriteLine( "Address: {0}", reply->Address->ToString() ); Console::WriteLine( "RoundTrip time: {0}", reply->RoundtripTime ); Console::WriteLine( "Time to live: {0}", reply->Options->Ttl ); Console::WriteLine( "Don't fragment: {0}", reply->Options->DontFragment ); Console::WriteLine( "Buffer size: {0}", reply->Buffer->Length ); } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
