using System;
using System.Text;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
public static void Main()
{
Console.WriteLine($"主线程:ttt线程Id:【{Environment.CurrentManagedThreadId}】t后台线程:【{Thread.CurrentThread.IsBackground}】t使用线程池:【{Thread.CurrentThread.IsThreadPoolThread}】t当前时间:【{DateTime_Now_ToString()}】");
string result = GetAwaitString("abc123").Result;
Console.WriteLine("结果:" + result);
Console.WriteLine("执行完毕:" + DateTime_Now_ToString());
Console.ReadLine();
}
public static string DateTime_Now_ToString()
{
return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff");
}
private static async Task GetAwaitString(string str)
{
Console.WriteLine("GetAwaitString await前:t线程Id:【{0}】t后台线程:【{1}】t使用线程池:【{2}】t当前时间:【{3}】",
Environment.CurrentManagedThreadId.ToString(),
Thread.CurrentThread.IsBackground,
Thread.CurrentThread.IsThreadPoolThread,
DateTime_Now_ToString());
var task = await Task.Run(() =>
{
Console.WriteLine("GetAwaitString await后:t线程Id:【{0}】t后台线程:【{1}】t使用线程池:【{2}】t当前时间:【{3}】",
Environment.CurrentManagedThreadId.ToString(),
Thread.CurrentThread.IsBackground,
Thread.CurrentThread.IsThreadPoolThread,
DateTime_Now_ToString());
return str + "," + DateTime_Now_ToString();
});
return task;
}
}
}