异步编程
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace awaitasynv1
{
internal class Program
{
static async Task Main(string[] args)
{
//string filename = @"1.txt";
//StringBuilder sb = new StringBuilder();
//System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
//stopwatch.Start();
//for (int i = 0; i < 10000; i++)
//{
// sb.AppendLine("hello");
//}
//File.WriteAllText(filename, sb.ToString());
//string s = File.ReadAllText(filename);
//Console.WriteLine(s);
//stopwatch.Stop();
//Console.WriteLine(stopwatch.Elapsed.Milliseconds);
string filename = @"1.txt";
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10000; i++)
{
sb.AppendLine("hello");
}
await File.WriteAllTextAsync(filename, sb.ToString());
//Task<string> t = File.ReadAllTextAsync(filename);
//string b = await t;
//自动把返回值从 Task取出来
string s = await File.ReadAllTextAsync(filename);
Console.WriteLine(s);
stopwatch.Stop();
Console.WriteLine(stopwatch.Elapsed.Milliseconds);
}
}
}
在线程里进行异步调用
using System;
using System.IO;
using System.Threading;
namespace async03
{
internal class Program
{
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem(async (obj) =>
{
while(true)
{
//Console.WriteLine("xxxxxxxxxx");
await File.WriteAllTextAsync("1.txt", "111111");
}
});
Console.Read();
}
}
}
await,async是语法糖,最终编译成状态机调用
async标记的方法中没有await会被当做同步方法处理
延时
await Task.Delay();
CancellationToken
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Program
{
static async Task Main(string[] args)
{
CancellationTokenSource cts = new CancellationTokenSource();
//定时两秒取消
//cts.CancelAfter(2000);
CancellationToken cToken = cts.Token;
DownloadAsync3("https://www.baidu.com", 1111,cToken);
while(Console.ReadLine() != "q")
{
}
cts.Cancel();
Console.ReadLine();
}
static async Task Main1(string[] args)
{
CancellationTokenSource cts = new CancellationTokenSource();
//cts.CancelAfter(2000);
CancellationToken cToken = cts.Token;
await DownloadAsync3("https://www.baidu.com", 1111, cToken);
while (Console.ReadLine() != "q")
{
}
cts.Cancel();
Console.ReadLine();
}
static async Task DownloadAsync(string url,int n)
{
using (HttpClient client = new HttpClient())
{
for (int i = 0; i < n; i++)
{
string html = await client.GetStringAsync(url);
Console.WriteLine($"{DateTime.Now}:{html}");
}
}
}
static async Task DownloadAsync2(string url, int n,CancellationToken cancellationToken )
{
using (HttpClient client = new HttpClient())
{
for (int i = 0; i < n; i++)
{
string html = await client.GetStringAsync(url);
Console.WriteLine($"{DateTime.Now}:{html}");
#region 请求被取消情况
//if(cancellationToken.IsCancellationRequested)
//{
// Console.WriteLine("please cancel");
// break;
//}
cancellationToken.ThrowIfCancellationRequested();
#endregion
}
}
}
/// <summary>
/// 手动输入q取消
/// </summary>
/// <param name="url"></param>
/// <param name="n"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
static async Task DownloadAsync3(string url, int n, CancellationToken cancellationToken)
{
using (HttpClient client = new HttpClient())
{
for (int i = 0; i < n; i++)
{
var resp = await client.GetAsync(url);
string html = await resp.Content.ReadAsStringAsync();
Console.WriteLine($"{DateTime.Now}:{html}");
if(cancellationToken.IsCancellationRequested)
{
Console.WriteLine("please cancel");
break;
}
}
}
}
}
}