Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WebClient不断破坏我的线程,但没有引发异常_C#_Multithreading_Crash_Webclient_Downloadstring - Fatal编程技术网

C# WebClient不断破坏我的线程,但没有引发异常

C# WebClient不断破坏我的线程,但没有引发异常,c#,multithreading,crash,webclient,downloadstring,C#,Multithreading,Crash,Webclient,Downloadstring,我正在使用WebClient下载html文件: private static string PullHTML(string url) { string data = null; if (url != null) { if (!WebCache.TryGetValue(url, out data)) {

我正在使用
WebClient
下载html文件:

        private static string PullHTML(string url)
        {
            string data = null;
            if (url != null)
            {
                if (!WebCache.TryGetValue(url, out data))
                {
               //THIS CODE IS REACHED
                    try
                    {
                        using (QuickWebClient wc = new QuickWebClient())
                        {

                            data = wc.DownloadString(url); //Debugging ends here
                        }
                    }
                    catch (WebException e)
                    {
               //THIS CODE IS NEVER REACHED
                        Console.WriteLine(e.Message);
                        data = e.Message;
                    }
               //NEITHER IS THIS
                    WebCache.Add(url, data);
                }
            }
            return data;
        }


这是在一个单独的线程中运行的(为了调试的目的,我重载了它,这个问题以前就开始了)。现在,这段代码似乎经常会使正在运行的线程崩溃。线基本上停止了。我无法调试它,因为调试器将在到达
DownloadString
行时立即离开。就好像我按了F5一样。没有抛出异常。线程不继续。发生了什么事?

打开“所有异常都中断”。我一步一步地浏览了代码。它自己取消了调试。“它自己取消了调试”通常意味着它在阻塞调用中卡住了。您是否打算使用
if(!WebCache.TryGetValue(url,out data))
尝试
try
catch
?您是否正在输入if
?是的,我正在输入if。
        private class QuickWebClient : WebClient
        {
            protected override WebRequest GetWebRequest(Uri uri)
            {
                WebRequest w = base.GetWebRequest(uri);
                w.Timeout = 5 * 60 * 1000;
                return w;
            }
        }