.NET C# 线程异常处理
我以为我理解这一点,并且有点不好意思问,但是,有人可以向我解释为什么以下代码的异常处理程序中的断点没有命中吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在创建一个线程,将其设置为后台线程,然后启动它。然后你的“主”线程就完成了。因为新线程是后台线程,所以它不会使进程保持活动状态 - 因此整个进程会在后台线程遇到任何问题之前完成。
如果您在
Main
方法中编写:或不将新线程设置为后台线程,则应命中断点。
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:
in your
Main
method, or don't set the new thread to be a background thread, you should hit the breakpoint.您的线程是后台线程(您自己设置)。这意味着当应用程序完成时,线程将被关闭而没有机会完成。
因为您的
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.很可能您的后台线程在 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.
由于新线程的
IsBackground
为 true,因此它不会阻止进程终止。在Main()
结束时,唯一的前台线程终止,在其他线程到达此之前关闭进程。Since the new thread's
IsBackground
is true, it won't prevent the process from terminating. At the end ofMain()
the only foreground thread terminates, shutting down the process before the other thread gets that far.可能是因为主线程在工作线程之前结束。尝试
在
Main
方法的末尾添加。Probably because the main thread is ending before the worker thread. Try adding
at the end of your
Main
method.