Skip to content

Instantly share code, notes, and snippets.

@SirRufo
Created November 20, 2017 01:33
Show Gist options
  • Save SirRufo/7f16b6a8841a694fab8c4c267053c260 to your computer and use it in GitHub Desktop.
Save SirRufo/7f16b6a8841a694fab8c4c267053c260 to your computer and use it in GitHub Desktop.
static void Main(string[] args)
{
var cts = new CancellationTokenSource();
var token = cts.Token;
var queryTask = QueryWebServiceInLoop("http://webapi/endpoint", token)
.ContinueWith(t =>
{
if (t.Status == TaskStatus.Faulted)
Console.WriteLine("faulted.");
});
Console.ReadLine();
cts.Cancel();
queryTask.GetAwaiter().GetResult();
}
static async Task QueryWebServiceInLoop(string url, CancellationToken cancellationToken = default(CancellationToken))
{
using (var client = new HttpClient())
{
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
var response = await client.GetAsync(url,cancellationToken).ConfigureAwait(false);
await Task.Delay(2000, cancellationToken).ConfigureAwait(false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment