.NET C# 线程异常处理

发布于 2024-10-24 07:28:02 字数 721 浏览 1 评论 0原文

我以为我理解这一点,并且有点不好意思问,但是,有人可以向我解释为什么以下代码的异常处理程序中的断点没有命中吗?

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        Thread testThread = new Thread(new ParameterizedThreadStart(TestDownloadThread));
        testThread.IsBackground = true;
        testThread.Start();
    }

    static void TestDownloadThread(object parameters)
    {
        WebClient webClient = new WebClient();

        try
        {
            webClient.DownloadFile("foo", "bar");
        }
        catch (Exception e)
        {
            System.Console.WriteLine("Error downloading: " + e.Message);
        }
    }

I thought I understood this, and am a bit embarrassed to be asking, but, can someone explain to me why the breakpoint in the exception handler of following code is not hit?

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        Thread testThread = new Thread(new ParameterizedThreadStart(TestDownloadThread));
        testThread.IsBackground = true;
        testThread.Start();
    }

    static void TestDownloadThread(object parameters)
    {
        WebClient webClient = new WebClient();

        try
        {
            webClient.DownloadFile("foo", "bar");
        }
        catch (Exception e)
        {
            System.Console.WriteLine("Error downloading: " + e.Message);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

别挽留 2024-10-31 07:28:02

您正在创建一个线程,将其设置为后台线程,然后启动它。然后你的“主”线程就完成了。因为新线程是后台线程,所以它不会使进程保持活动状态 - 因此整个进程会在后台线程遇到任何问题之前完成。

如果您在 Main 方法中编写:

testThread.Join();

不将新线程设置为后台线程,则应命中断点。

You're creating a thread, setting it to be a background thread, and starting it. Your "main" thread is then finishing. Because the new thread is a background thread, it doesn't keep the process alive - so the whole process is finishing before the background thread runs into any problems.

If you write:

testThread.Join();

in your Main method, or don't set the new thread to be a background thread, you should hit the breakpoint.

还不是爱你 2024-10-31 07:28:02

您的线程是后台线程(您自己设置)。这意味着当应用程序完成时,线程将被关闭而没有机会完成。

因为您的 main 没有更多内容,所以它会立即退出,并且应用程序和线程都会终止。

如果您要添加 testThread.Join() 调用,那么您的线程将在应用程序退出之前完成,并且您会看到捕获到的异常。

Your thread is a Background thread (you set it so yourself). This means that when the application completes, the thread will be shut down without being given chance to complete.

Because your main has no further content, it exits immediately and both the application and your thread are terminated.

If you were to add a testThread.Join() call then your thread would complete before the app exited and you'd see the exception caught.

红焚 2024-10-31 07:28:02

很可能您的后台线程在 Main 退出之前尚未启动 - 启动新线程可能需要一段时间。由于附加线程是后台线程,因此它会在 Main 退出时终止。

出于演示目的,您可以让主方法等待 - 例如,等待用户输入。这将允许后台线程开始运行。

Most likely your background thread isn't started before Main exits - starting a new thread may take a while. Since the additional thread is a background thread it is terminated when Main exits.

For demo purposes, you could have your main method wait - e.g. on user input. This would allow the background thread to start running.

遮云壑 2024-10-31 07:28:02

由于新线程的 IsBackground 为 true,因此它不会阻止进程终止。在 Main() 结束时,唯一的前台线程终止,在其他线程到达此之前关闭进程。

Since the new thread's IsBackground is true, it won't prevent the process from terminating. At the end of Main() the only foreground thread terminates, shutting down the process before the other thread gets that far.

全部不再 2024-10-31 07:28:02

可能是因为主线程在工作线程之前结束。尝试

Console.ReadKey();

Main 方法的末尾添加。

Probably because the main thread is ending before the worker thread. Try adding

Console.ReadKey();

at the end of your Main method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文