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
var tokenSource = new CancellationTokenSource(); | |
var token = tokenSource.Token; | |
var task = Task.Factory.StartNew(() => | |
{ | |
for (var i = 0; i < 1000; i++) | |
{ | |
System.Threading.Thread.Sleep(1000); | |
if (token.IsCancellationRequested) | |
{ | |
Console.WriteLine("Abort mission success!"); |
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
// Three things to note in the signature: | |
// - The method has an async modifier. | |
// - The return type is Task or Task<T>. (See "Return Types" section.) | |
// Here, it is Task<int> because the return statement returns an integer. | |
// - The method name ends in "Async." | |
async Task<int> AccessTheWebAsync() | |
{ | |
// You need to add a reference to System.Net.Http to declare client. | |
HttpClient client = new HttpClient(); |