If you can't use a BackgroundWorker and you're targeting .NET 4.5 you can wrap that method inside a Task.
public static Task<bool> CheckInternetConnectionAsync( )
{
return Task<bool>.Run( ( ) => {
try
{
using ( var client = new WebClient( ) )
using ( var stream = client.OpenRead( "http://www.google.com" ) )
{
return true;
}
}
catch
{
return false;
}
} );
}
And whenever you need to check for internet connection do it like this ...
static async void CheckInternetConnection( )
{
//just an example how to read a value from Task
bool hasConnection = await CheckInternetConnectionAsync( );
}