标签:inf ret ide tps 线程 异步 官方 class guid
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}");
Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}:{SayAsync().Status}");
Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}");
Console.ReadKey(true);
}
public static async Task<string> SayAsync()
{
Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}_SayAsync");
var s = await Task<string>.Run(async () =>
{
await Task.Delay(TimeSpan.FromSeconds(2));
Console.WriteLine("---------------两秒后----------------");
Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}");
return "vvull";
});
Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}:{s}");
Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}");
return s;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}");
Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}:{SayAsync().Result}");//阻塞主线程
Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}");
Console.ReadKey(true);
}
public static async Task<string> SayAsync()
{
Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}_SayAsync");
var s = await Task<string>.Run(async () =>
{
await Task.Delay(TimeSpan.FromSeconds(2));
Console.WriteLine("---------------两秒后----------------");
Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}");
return "vvull";
});
Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}:{s}");
Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}");
return s;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->主线程");
giaoAsync();
Thread.Sleep(3000);
Console.WriteLine("---------------三秒后----------------");
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->主线程");
Console.ReadKey();
}
static async Task giaoAsync()
{
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao");
//Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao:{await SayAsync()}");//本句以后由新线程接管
//Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao*");
var s = await SayAsync();//由新线接管
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao**_{s}");
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao**");
var ss = await SayAsync();//由新线接管
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao***_{ss}");
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao***");
}
public static async Task<string> SayAsync()
{
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->SayAsync");
var s = await Task<string>.Run(async () =>
{
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->SayAsync*");
await Task.Delay(TimeSpan.FromSeconds(0.001));
Console.WriteLine("---------------两秒后----------------");
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->SayAsync**");
return "vvull";
});
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->SayAsync**_{s}");
Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->SayAsync**");
return s;
}
}
标签:inf ret ide tps 线程 异步 官方 class guid
原文地址:https://www.cnblogs.com/vvull/p/13218369.html