Calling Method Asynchronously With A Timeout





4.00/5 (5 votes)
How to call a method asynchronously but have a timeout
As part of an ongoing project I am just about to finish, I had a need to use a 3rd party web service. Now I am quite a cautious sort of chap when it comes to using web services. So I generally try and ensure that I try and make my code as robust as possible when dealing with such code.
For example, when calling a web service, there is no guarantee that the web server is not going to die, after all it could be a web service that is hosted on a single server. Of course, if the web service is from a reputable source, it's probably going to be a web farm that you're dealing with. Nevertheless, it’s good to plan for problems such as a timeout, lack of results, remote connection denials, security issues. These calls all lead into problems when dealing with a web service.
To this end, I have written the following bit of code to allow me to call a method asynchronously but have a timeout. This code does not use a web service, but rather shows the pattern that could be used to call any method asynchronously with a timeout. Which of course fits well with the idea of calling a web service, call the web service asynchronously, wait for a reasonable time, then see if the results are available. If they are, use them, if not alert the user in whatever manner you have opted for.
Anyway the code is fairly simple and is as follows:
using System;
using System.Collections.Generic;
using System.Text;
namespace Explorer3D
{
class AsynchMethodCall
{
public AsynchMethodCall()
{
string[] usersFetched;
//call the delegate asynchronously and wait for the results
//from the asynch call.
fetchNumberOfUsersDelegate fetchUsers = getNumberOfUsers;
IAsyncResult asynchResult = fetchNumberOfUsersDelegate.BeginInvoke(1000000,
null, null);
while (!asynchResult.AsyncWaitHandle.WaitOne(5000, false))
{
//waiting for result for exactly 5 seconds
}
//try and catch any nasty exception that may have occurred
try
{
usersFetched = fetchUsers.EndInvoke(asynchResult);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// The delegate that will be called asynchronously
/// </summary>
internal delegate string[] fetchNumberOfUsersDelegate(int someValue);
/// <summary>
/// A supposedly long running method
/// </summary>
/// <param name=”numberOfUsersToFetch”>number of users to fetch</param>
/// <returns>a string array of users fetched</returns>
private string[] getNumberOfUsers(long numberOfUsersToFetch)
{
//do the long bit of work, say gather the number of users
//indicated by the numberOfUsersToFetch parameter from a remote
//database
}
}
}