The Ping class is new to .NET 2.0 and it let’s you ping remote addresses much the same way as the ping.exe command line tool does. It’s a great way to check the availability of different kinds of remote servers over the network in a very low cost and fast way. The most important result a ping can give you is the ping time. The ping time gives you an indication on the network speed between you and the server.

I’ve written a very small and easy to use method that returns the ping time of a given URL.

using System.Net.NetworkInformation;

 

/// <summary>

/// Pings the specified URL within the timeout period.

/// </summary>

/// <param name="url">The URL to ping.</param>

/// <param name="timeout">Number of milliseconds.</param>

/// <returns>Returns the ping time in milliseconds.</returns>

private static long PingTime(string url, int timeout)

{

  try

  {

    using (Ping ping = new Ping ())

    {

      PingReply reply = ping.Send(url, timeout);

      return reply.RoundtripTime;

    }

  }

  catch (System.Net.Sockets.SocketException)

  {

    return -1;

  }

}

You can use the method from whenever you want like the following an ASP.NET webpage.

Response.Write(PingTime("http://www.google.com", 2000));

In ASP.NET 2.0 it is now possible to create asynchronous pages in a very simple and easy way. It gives you a whole new abstraction layer on top of the managed thread pool that let’s any webpage take advantage of multithreading. On high volume websites it means much higher performance. The single coolest thing about it is that you build the web pages as you normally would, but move some of the code into two separate methods. That means that there is no real learning curve, because of the simplicity.

The only thing needed to make is the use of classes that have asynchronous methods. It could be calling web services, doing database queries, creating a web request or many other things. In this example I’ve chosen a web request because it is the simplest to demo. The page makes an asynchronous request to a website and writes the HTTP status code to the response stream.

First of all, we have to tell the page that is has to work in asynchronous mode. We do that by adding the Async=”true” attribute to the page declaration.

<%@ Page Language="C#" Async="true" CodeFile="asyncpage.aspx.cs" Inherits="asyncpage" %>

Then we just have to add two methods and register them in the Page_Load event handler.

using System.Net;

 

protected void Page_Load(object sender, EventArgs e)

{   

  AddOnPreRenderCompleteAsync(BeginGetAsyncData, EndGetAsyncData);

}

 

private HttpWebRequest _Request = (HttpWebRequest)WebRequest.Create("http://www.google.com");

 

private IAsyncResult BeginGetAsyncData(Object src, EventArgs args, AsyncCallback cb, Object state)

{

  return _Request.BeginGetResponse(cb, state);

}

>

 

private void EndGetAsyncData(IAsyncResult ar)

{

  using (HttpWebResponse response = (HttpWebResponse)_Request.EndGetResponse(ar))

  {

    Response.Write(response.StatusCode);

  }
}

As you can see from the example, only very few extra lines of code is needed to make the page render asynchronously. Even though they are far from alike, it reminds me of the BackgroundWorker in Windows Forms.