Task.Factory.StartNew() 不适合我

发布于 2024-12-25 23:36:11 字数 468 浏览 3 评论 0原文

我写了这个小应用程序。由于某种原因,当我运行这个程序时,我无法打印“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾忌 2025-01-01 23:36:11

您确定在看到输出之前程序没有关闭吗?因为这对我来说效果很好。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        private static void Main(string[] args)
        {
            Task.Factory.StartNew(Do);
            Console.Read();
        }

        private static void Do()
        {
            Console.WriteLine("Hello from a thread");
        }
    }
}

编辑:添加了我对此的回应所写的评论,包括我对文本未打印的原因的推理。

要么是因为应用程序在线程有可能将字符串输出到屏幕。也有可能您根本看不到它,因为它立即关闭。无论哪种方式,它与断点一起工作的原因是因为您可以使应用程序保持更长时间的活动。

Are you sure that the program simply isn't closing before you can see the output? Because this works fine for me.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        private static void Main(string[] args)
        {
            Task.Factory.StartNew(Do);
            Console.Read();
        }

        private static void Do()
        {
            Console.WriteLine("Hello from a thread");
        }
    }
}

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.

离线来电— 2025-01-01 23:36:11

试试这个。

using System;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Task.Factory.StartNew(Do);
            Console.ReadKey();
        }

        static void Do()
        {
            Console.WriteLine("Hello from a thread");
        }
    }
}

Try this.

using System;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Task.Factory.StartNew(Do);
            Console.ReadKey();
        }

        static void Do()
        {
            Console.WriteLine("Hello from a thread");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文