Task.Factory.StartNew() 不适合我
我写了这个小应用程序。由于某种原因,当我运行这个程序时,我无法打印“Hello from a thread”。但是,如果我调试它并在 Do() 方法中放置断点,它就会打印。
有什么想法吗?
using System;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
internal class Program
{
private static void Main(string[] args)
{
Task.Factory.StartNew(Do);
}
private static void Do()
{
Console.WriteLine("Hello from a thread");
}
}
}
I wrote this small application. for some reason i am not being able to print "Hello from a thread" when i run this program. however if i debug it and put a breakpoint inside Do() method, it does print.
Any ideas?
using System;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
internal class Program
{
private static void Main(string[] args)
{
Task.Factory.StartNew(Do);
}
private static void Do()
{
Console.WriteLine("Hello from a thread");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定在看到输出之前程序没有关闭吗?因为这对我来说效果很好。
编辑:添加了我对此的回应所写的评论,包括我对文本未打印的原因的推理。
要么是因为应用程序在线程有可能将字符串输出到屏幕。也有可能您根本看不到它,因为它立即关闭。无论哪种方式,它与断点一起工作的原因是因为您可以使应用程序保持更长时间的活动。
Are you sure that the program simply isn't closing before you can see the output? Because this works fine for me.
Edit: Added the comment I wrote in response to this, including my reasoning to why the text wasn't printed.
Its either because the application closes down before the thread has the possibility of outputting the string to the screen. It's also possible that you simply cant see it because it closes straight away. Either way the reason it worked with the breakpoint is because you keep the application alive longer.
试试这个。
Try this.