今天有点疑惑就写了个测试的代码,发现控制台和Winform中不一样
比如:
控制台:
...Main(){ Test(); }
private static async void Test()
{
Show("Test.Before");
await Task.Run(() => { Thread.Sleep(2000); Show("Test.Task"); });
Show("Test.After");//输出字符串和当前线程
}
private static void Show(string str)
{
Console.WriteLine($"{str}:{Thread.CurrentThread.ManagedThreadId}\r\n");
}
输出:
Test.Before:1
Test.Task:3
Test.After:3
Winform中:
...test(){ Test(); }
private async void Test()
{
Show("Test.Before");
await Task.Run(() => { Thread.Sleep(2000); });
Show("Test.After");//输出字符串和当前线程
}
public void Show(string str)
{
richTextBox1.AppendText($"{str}:{Thread.CurrentThread.ManagedThreadId}\r\n");
}
输出:
Test.Before:1
Test.After:1
为什么这两个不一样呢?新手求解答
RISEBY
相关分类