Performance Tips for Asynchronous Development in C#: Listing 2.

Code for caching Task<bool> results.

private static Task<bool> trueTask;
private static Task<bool> falseTask;

public static Task<bool> SiteIsUpAsyncCache(string url)
  if (url.Length % 2 == 0)
    return trueTask ?? (trueTask = CreateTaskResult(true));
  return falseTask ?? (falseTask = CreateTaskResult(false));

private static Task<T> CreateTaskResult<T>(T result)
  var tcs = new TaskCompletionSource<T>();
  return tcs.Task;

About the Author

Patrick Steele is a senior .NET developer with Billhighway in Troy, Mich. A recognized expert on the Microsoft .NET Framework, he’s a former Microsoft MVP award winner and a presenter at conferences and user group meetings.

comments powered by Disqus


Subscribe on YouTube