c# - 如果有一些前台线程在控制台上打印一些东西,在主线程完成他的工作后控制台窗口会关闭吗?
问题描述
如果有一些前台线程在控制台上打印一些东西,在主线程完成他的工作后控制台窗口会关闭吗?那么基本上在其他线程中打印的文本会显示在控制台上吗?
类似于以下代码:
public static void Main(string[] args)
{
Console.WriteLine("string");
var threads = new Thread[5];
for (int i = 0; i < threads.Length; ++i)
{
threads[i] = new Thread(() => Console.WriteLine("smth"));
threads[i].Start();
}
}
解决方案
根据您运行代码的方式(.NET Framework、.NET Core 或 Mono),您应该调整睡眠时间。
public static void Main(string[] args)
{
Console.WriteLine("string");
var threads = new Thread[5];
for (int i = 0; i < threads.Length; ++i)
{
threads[i] = new Thread(() => {
Thread.Sleep(1000);
Console.WriteLine("smth");
}) { IsBackground = true };
threads[i].Start();
}
}
推荐阅读
- reactjs - 将 webpack 构建与 ASP.NET Core 3.0 集成的最佳方式?
- json - 向 JMeter 中的多个线程提供 1 个 JDBC 请求结果
- ios - 如何在 ionic 中使用 iOS 平台实现内联回复?
- javascript - 声明一个不是 var、const 或 let 的变量
- javascript - 如何在 javascript 中使用正则表达式在字符串中查找子字符串?
- excel - Vlookup 或索引匹配一系列单元格 - 第一个、第二个、第三个、第四个等
- java - 应用程序没有采用我设置的布局 - 为什么?
- git - How can I rebase after a merge?
- nativescript - 有没有办法使用nativescript访问选项卡视图的IOS的badgeValue
- angular - Angular Signallar R 事件没有被破坏