Skip to content

Instantly share code, notes, and snippets.

Avatar

Zhang Zhe elitezhe

  • China
View GitHub Profile
@elitezhe
elitezhe / task_cancel.cs
Created Apr 11, 2016
From http://www.cnblogs.com/yunfeifei/p/4106318.html 使用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++)
{
System.Threading.Thread.Sleep(1000);
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.