Created
November 20, 2017 01:33
-
-
Save SirRufo/7f16b6a8841a694fab8c4c267053c260 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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