任务 Async/Await ... 不明白
Tasks Async/Await ... don't get it
我在 C# 中实现了这样的多任务处理:
using System;
using System.Threading;
using System.Threading.Tasks;
namespace TasksAsyncAwait
{
class Program
{
static async Task Main(string[] args)
{
CancellationTokenSource cancellationTokenSource
= new CancellationTokenSource();
CancellationToken cancellationToken = cancellationTokenSource.Token;
Task taskOne = Task.Factory.StartNew(() =>
{
Console.WriteLine("Task 1 start");
for (int i = 0; i < 10; i++)
{
cancellationToken.ThrowIfCancellationRequested();
Console.WriteLine($"T1: {i}");
cancellationToken.WaitHandle.WaitOne(500);
}
Console.WriteLine("Task 1 complete");
}, cancellationToken);
Task taskTwo = Task.Factory.StartNew(() =>
{
Console.WriteLine("Task 2 start");
for (int i = 0; i < 5; i++)
{
cancellationToken.ThrowIfCancellationRequested();
Console.WriteLine($"T2: {i}");
cancellationToken.WaitHandle.WaitOne(300);
}
Console.WriteLine("Task 2 complete");
}, cancellationToken);
Task.WaitAll(taskTwo, taskOne);
Console.WriteLine("Program done.");
}
}
}
有人告诉我,尽管代码运行良好且易于理解,但它已经过时且已弃用,因此风格不佳,我需要重写此代码以遵循 async/await 方案。我读了很多关于如何使用 async/await 的文章,一些教程甚至涵盖了如何在控制台应用程序中使用它,但坦率地说,我一个字也不懂。请告诉我如何将我的代码转换为 async/await 样式。
你在这里:
using System;
using System.Threading;
using System.Threading.Tasks;
namespace TasksAsyncAwait
{
class Program
{
static async Task Main()
{
var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
Task taskOne = Task.Run(async () =>
{
Console.WriteLine("Task 1 start");
for (int i = 0; i < 10; i++)
{
cancellationToken.ThrowIfCancellationRequested();
Console.WriteLine($"T1: {i}");
await Task.Delay(500, cancellationToken);
}
Console.WriteLine("Task 1 complete");
}, cancellationToken);
Task taskTwo = Task.Run(async () =>
{
Console.WriteLine("Task 2 start");
for (int i = 0; i < 5; i++)
{
cancellationToken.ThrowIfCancellationRequested();
Console.WriteLine($"T2: {i}");
await Task.Delay(300, cancellationToken);
}
Console.WriteLine("Task 2 complete");
}, cancellationToken);
await Task.WhenAll(taskOne, taskTwo);
Console.WriteLine("Program done.");
}
}
}
无阻塞代码;异步等待无处不在!
我在 C# 中实现了这样的多任务处理:
using System;
using System.Threading;
using System.Threading.Tasks;
namespace TasksAsyncAwait
{
class Program
{
static async Task Main(string[] args)
{
CancellationTokenSource cancellationTokenSource
= new CancellationTokenSource();
CancellationToken cancellationToken = cancellationTokenSource.Token;
Task taskOne = Task.Factory.StartNew(() =>
{
Console.WriteLine("Task 1 start");
for (int i = 0; i < 10; i++)
{
cancellationToken.ThrowIfCancellationRequested();
Console.WriteLine($"T1: {i}");
cancellationToken.WaitHandle.WaitOne(500);
}
Console.WriteLine("Task 1 complete");
}, cancellationToken);
Task taskTwo = Task.Factory.StartNew(() =>
{
Console.WriteLine("Task 2 start");
for (int i = 0; i < 5; i++)
{
cancellationToken.ThrowIfCancellationRequested();
Console.WriteLine($"T2: {i}");
cancellationToken.WaitHandle.WaitOne(300);
}
Console.WriteLine("Task 2 complete");
}, cancellationToken);
Task.WaitAll(taskTwo, taskOne);
Console.WriteLine("Program done.");
}
}
}
有人告诉我,尽管代码运行良好且易于理解,但它已经过时且已弃用,因此风格不佳,我需要重写此代码以遵循 async/await 方案。我读了很多关于如何使用 async/await 的文章,一些教程甚至涵盖了如何在控制台应用程序中使用它,但坦率地说,我一个字也不懂。请告诉我如何将我的代码转换为 async/await 样式。
你在这里:
using System;
using System.Threading;
using System.Threading.Tasks;
namespace TasksAsyncAwait
{
class Program
{
static async Task Main()
{
var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
Task taskOne = Task.Run(async () =>
{
Console.WriteLine("Task 1 start");
for (int i = 0; i < 10; i++)
{
cancellationToken.ThrowIfCancellationRequested();
Console.WriteLine($"T1: {i}");
await Task.Delay(500, cancellationToken);
}
Console.WriteLine("Task 1 complete");
}, cancellationToken);
Task taskTwo = Task.Run(async () =>
{
Console.WriteLine("Task 2 start");
for (int i = 0; i < 5; i++)
{
cancellationToken.ThrowIfCancellationRequested();
Console.WriteLine($"T2: {i}");
await Task.Delay(300, cancellationToken);
}
Console.WriteLine("Task 2 complete");
}, cancellationToken);
await Task.WhenAll(taskOne, taskTwo);
Console.WriteLine("Program done.");
}
}
}
无阻塞代码;异步等待无处不在!