Doo*_*Dah 5 .net c# multithreading
我以为我理解了这一点,并且有点尴尬要问,但是,有人可以向我解释为什么以下代码的异常处理程序中的断点没有被命中?
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);
}
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 11
您正在创建一个线程,将其设置为后台线程,然后启动它.然后你的"主"线程就完成了.因为新线程是后台线程,所以它不会使进程保持活动状态 - 因此整个过程在后台线程遇到任何问题之前完成.
如果你写:
testThread.Join();
Run Code Online (Sandbox Code Playgroud)
在你的Main方法中,或者不将新线程设置为后台线程,你应该点击断点.