Skip to content

Instantly share code, notes, and snippets.


Zhang Zhe elitezhe

  • China
View GitHub Profile
elitezhe / task_cancel.cs
Created Apr 11, 2016
From 使用token和轮询方式在task执行过程中return,也可throw exception
View task_cancel.cs
var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
var task = Task.Factory.StartNew(() =>
for (var i = 0; i < 1000; i++)
if (token.IsCancellationRequested)
Console.WriteLine("Abort mission success!");
View async_await.cs
// 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();
You can’t perform that action at this time.