Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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# Winform应用程序随机退出_C#_.net_Winforms_Webclient_Html Agility Pack - Fatal编程技术网

C# Winform应用程序随机退出

C# Winform应用程序随机退出,c#,.net,winforms,webclient,html-agility-pack,C#,.net,Winforms,Webclient,Html Agility Pack,我有一个Winform应用程序随机崩溃。该应用程序使用HtmlAgility的WebClient解析html页面并从web服务器下载图像。我尝试使用try-Catch,但应用程序没有提供其他异常,这些异常可以告诉我导致崩溃的原因,除了“500内部服务器错误”。这里有一些信息。有人能帮我找到问题吗 谢谢 'ReImageApp.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\msco

我有一个Winform应用程序随机崩溃。该应用程序使用HtmlAgility的WebClient解析html页面并从web服务器下载图像。我尝试使用try-Catch,但应用程序没有提供其他异常,这些异常可以告诉我导致崩溃的原因,除了“500内部服务器错误”。这里有一些信息。有人能帮我找到问题吗

谢谢

    'ReImageApp.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'ReImageApp.exe' (Managed (v4.0.30319)): Loaded 'C:\ReImageProcess\bin\Debug\ReImageApp.exe', Symbols loaded.
'ReImageApp.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'ReImageApp.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'ReImageApp.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'ReImageApp.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'ReImageApp.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'ReImageApp.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'ReImageApp.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread '<No Name>' (0x27d0) has exited with code 0 (0x0).
The thread '<No Name>' (0x2148) has exited with code 0 (0x0).
The thread '<No Name>' (0x26e4) has exited with code 0 (0x0).
The thread '<No Name>' (0x2998) has exited with code 0 (0x0).
The thread '<No Name>' (0x1484) has exited with code 0 (0x0).
The thread '<No Name>' (0x239c) has exited with code 0 (0x0).
'ReImageApp.exe' (Managed (v4.0.30319)): Loaded 'C:\ReImageProcess\bin\Debug\HtmlAgilityPack.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x10e0) has exited with code 0 (0x0).
The thread '<No Name>' (0x2acc) has exited with code 0 (0x0).
The thread '<No Name>' (0x2904) has exited with code 0 (0x0).
The thread '<No Name>' (0x230c) has exited with code 0 (0x0).
The thread '<No Name>' (0xecc) has exited with code 0 (0x0).
The thread '<No Name>' (0x2adc) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x1f18) has exited with code 0 (0x0).
The thread '<No Name>' (0x24e4) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x205c) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x26c0) has exited with code 0 (0x0).
The thread '<No Name>' (0x2700) has exited with code 0 (0x0).
The thread '<No Name>' (0x1fc0) has exited with code 0 (0x0).
The thread '<No Name>' (0x2534) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x2168) has exited with code 0 (0x0).
The thread '<No Name>' (0x2bd0) has exited with code 0 (0x0).
The thread '<No Name>' (0x2754) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x22e0) has exited with code 0 (0x0).
The thread '<No Name>' (0x2a50) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x2bdc) has exited with code 0 (0x0).
The thread '<No Name>' (0x1c14) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x23d0) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x1174) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x11f4) has exited with code 0 (0x0).
The thread '<No Name>' (0x1ab8) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x2a38) has exited with code 0 (0x0).
The thread '<No Name>' (0x2674) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x16e8) has exited with code 0 (0x0).
The thread '<No Name>' (0x2710) has exited with code 0 (0x0).
The thread '<No Name>' (0x22f0) has exited with code 0 (0x0).
The thread '<No Name>' (0xd58) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0xf34) has exited with code 0 (0x0).
The thread '<No Name>' (0x1e80) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x2be4) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x1178) has exited with code 0 (0x0).
The thread '<No Name>' (0x1724) has exited with code 0 (0x0).
The thread '<No Name>' (0x2370) has exited with code 0 (0x0).
The thread '<No Name>' (0x2b90) has exited with code 0 (0x0).
The thread '<No Name>' (0x2434) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x2b4c) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x26d8) has exited with code 0 (0x0).
The thread '<No Name>' (0xb2c) has exited with code 0 (0x0).
The thread '<No Name>' (0x1428) has exited with code 0 (0x0).
The thread '<No Name>' (0x1138) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x1b0) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x2228) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x22c4) has exited with code 0 (0x0).
The thread '<No Name>' (0x1670) has exited with code 0 (0x0).
The thread '<No Name>' (0x166c) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x2828) has exited with code 0 (0x0).
The thread '<No Name>' (0x28c0) has exited with code 0 (0x0).
The thread '<No Name>' (0x1160) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x1d14) has exited with code 0 (0x0).
The thread '<No Name>' (0x12b8) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x1b48) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x2274) has exited with code 0 (0x0).
The thread '<No Name>' (0x1ca0) has exited with code 0 (0x0).
The thread '<No Name>' (0x904) has exited with code 0 (0x0).
The thread '<No Name>' (0x16f0) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x2928) has exited with code 0 (0x0).
The thread '<No Name>' (0x4b8) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x261c) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x1f20) has exited with code 0 (0x0).
The thread '<No Name>' (0x2a14) has exited with code 0 (0x0).
The thread '<No Name>' (0x1ecc) has exited with code 0 (0x0).
The thread '<No Name>' (0x2814) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x27a0) has exited with code 0 (0x0).
The thread '<No Name>' (0x181c) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x2a30) has exited with code 0 (0x0).
The thread '<No Name>' (0x2a18) has exited with code 0 (0x0).
The thread '<No Name>' (0x26c4) has exited with code 0 (0x0).
The thread '<No Name>' (0x11e0) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x247c) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x2960) has exited with code 0 (0x0).
The thread '<No Name>' (0x262c) has exited with code 0 (0x0).
The thread '<No Name>' (0x114c) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x2a80) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x254c) has exited with code 0 (0x0).
The thread '<No Name>' (0x1898) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
The thread '<No Name>' (0x2a54) has exited with code 0 (0x0).
The thread '<No Name>' (0x2a78) has exited with code 0 (0x0).
The thread '<No Name>' (0x2bc0) has exited with code 0 (0x0).
The thread '<No Name>' (0x2690) has exited with code 0 (0x0).
The thread '<No Name>' (0x201c) has exited with code 0 (0x0).
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.dll
A first chance exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll
The program '[10296] ReImageApp.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).
“ReImageApp.exe”(托管(v4.0.30319)):加载的“C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__B77A5C561934; E089\mscorlib.dll”跳过了加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。
“ReImageApp.exe”(托管(v4.0.30319)):已加载“C:\ReImageProcess\bin\Debug\ReImageApp.exe”,已加载符号。
“ReImageApp.exe”(托管(v4.0.30319)):加载的“C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0_uuB77A5C561934E089\System.Windows.Forms.dll”跳过了加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。
“ReImageApp.exe”(托管(v4.0.30319)):加载的“C:\Windows\Microsoft.Net\assembly\GAC\U MSIL\System.Drawing\v4.0\U 4.0.0\Uuu b03f5f7f11d50a3a\System.Drawing.dll”跳过了加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。
“ReImageApp.exe”(托管(v4.0.30319)):加载的“C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0_b77a5c561934e089\System.dll”跳过了加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。
“ReImageApp.exe”(托管(v4.0.30319)):加载的“C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll”跳过了加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。
“ReImageApp.exe”(托管(v4.0.30319)):加载的“C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0_b77a5c561934e089\System.Xml.dll”跳过了加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。
“ReImageApp.exe”(托管(v4.0.30319)):加载的“C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\10.0.0_uB03F5F7F11D50A3A\Microsoft.VisualStudio.Debugger.Runtime.dll”跳过了加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。
“ReImageApp.exe”(托管(v4.0.30319)):加载的“C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0_b77a5c561934e089\System.Core.dll”跳过了加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。
线程“”(0x27d0)已退出,代码为0(0x0)。
线程“”(0x2148)已退出,代码为0(0x0)。
线程“”(0x26e4)已退出,代码为0(0x0)。
线程“”(0x2998)已退出,代码为0(0x0)。
线程“”(0x1484)已退出,代码为0(0x0)。
线程“”(0x239c)已退出,代码为0(0x0)。
“ReImageApp.exe”(托管(v4.0.30319)):加载了“C:\ReImageProcess\bin\Debug\HtmlAgilityPack.dll”,跳过了加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。
System.dll中发生类型为“System.Reflection.TargetInvocationException”的首次意外异常
System.dll中发生类型为“System.Reflection.TargetInvocationException”的首次意外异常
线程“”(0x10e0)已退出,代码为0(0x0)。
线程“”(0x2acc)已退出,代码为0(0x0)。
线程“”(0x2904)已退出,代码为0(0x0)。
线程“”(0x230c)已退出,代码为0(0x0)。
线程“”(0xecc)已退出,代码为0(0x0)。
线程“”(0x2adc)已退出,代码为0(0x0)。
System.dll中发生类型为“System.Reflection.TargetInvocationException”的首次意外异常
System.dll中发生类型为“System.Reflection.TargetInvocationException”的首次意外异常
线程“”(0x1f18)已退出,代码为0(0x0)。
线程“”(0x24e4)已退出,代码为0(0x0)。
System.dll中发生类型为“System.Reflection.TargetInvocationException”的首次意外异常
线程“”(0x205c)已退出,代码为0(0x0)。
System.dll中发生类型为“System.Reflection.TargetInvocationException”的首次意外异常
System.dll中发生类型为“System.Reflection.TargetInvocationException”的首次意外异常
线程“”(0x26c0)已退出,代码为0(0x0)。
线程“”(0x2700)已退出,代码为0(0x0)。
线程“”(0x1fc0)已退出,代码为0(0x0)。
线程“”(0x2534)已退出,代码为0(0x0)。
System.dll中发生类型为“System.Reflection.TargetInvocationException”的首次意外异常
线程“”(0x2168)已退出,代码为0(0x0)。
线程“”(0x2bd0)已退出,代码为0(0x0)。
线程“”(0x2754)已退出,代码为0(0x0)。
System.dll中发生类型为“System.Reflection.TargetInvocationException”的首次意外异常
线程“”(0x22e0)已退出,代码为0(0x0)。
线程“”(0x2a50)已退出,代码为0(0x0)。
System.dll中发生类型为“System.Reflection.TargetInvocationException”的首次意外异常
System.dll中发生类型为“System.Reflection.TargetInvocationException”的首次意外异常
System.dll中发生类型为“System.Reflection.TargetInvocationException”的首次意外异常
线程“”(0x2bdc)已退出,代码为0(0x0)。
线程“”(0x1c14)已退出,代码为0(0x0)。
System.dll中发生类型为“System.Reflection.TargetInvocationException”的首次意外异常
线程“”(0x23d0)已退出,代码为0(0x0)。
System.dll中发生类型为“System.Reflection.TargetInvocationException”的首次意外异常
线程“”(0x1174)已退出,代码为0(0x0)。
System.dll中发生类型为“System.Reflection.TargetInvocationException”的首次意外异常
线程“”(0x11f4)已退出,代码为0(0x0)。
线程“”(0x1ab8)已退出,代码为0(0x0)。
“System.Reflection.Target”类型的第一次意外异常
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
using System.Configuration;
using System.ComponentModel;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
using System.Drawing;
using ImageManipulation;
using System.Drawing.Imaging;

namespace ReImageApp
{
    class DownLoadImages
    {
        TextWriter tw2;
        object objLocker = new object();
        Dictionary<string, string> dc = new Dictionary<string, string>();
        private Queue<string> _downloadImageUrls = new Queue<string>();
        private Queue<string> _loggerQueue = new Queue<string>();

        public DownLoadImages()
        {

        }

        public DownLoadImages(TextWriter tw)
        {
            tw2 = tw;
        }

        public void AddImage2Queue(string url, string path)
        {
            _downloadImageUrls.Enqueue(url);
            dc.Add(url, path);
        }


        public void RunHtmlAgilityPackThread(string url)
        {
            try
            {
                Thread.Sleep(Convert.ToInt16(ConfigurationManager.GetAppSetting("PauseTime")) * 1000);
                WebClient linkclient = new WebClient();
                linkclient.DownloadDataCompleted += DownloadDataCompleted;
                linkclient.DownloadDataAsync(new Uri(url), url);
            }
            catch (Exception ex)
            {
                    _loggerQueue.Enqueue("Error in RunHtmlAgilityPackThread for url -- " + url + "  " + (ex.InnerException == null? "" : ex.InnerException.Message));
            }
        }

        private void DownloadDataCompleted(object sender,
            DownloadDataCompletedEventArgs e)
        {
            var url = (string)e.UserState;

            try
            {
                byte[] dbytes = e.Result;

                string responseStr = System.Text.Encoding.ASCII.GetString(dbytes);
                HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
                document.LoadHtml(responseStr);

                if (document.DocumentNode != null)
                {
                    IList<string> ilImages = (document.DocumentNode.Descendants("img").Select(x => x.Attributes["src"].Value).Distinct().ToList());

                    if (ilImages.Count == 0)
                    {
                            _loggerQueue.Enqueue("Warning (contains not image) in RunHtmlAgilityPackThread-- " + url);

                        return;
                    }

                    GetImageUrls(ilImages, url);
                }
                else
                {
                        _loggerQueue.Enqueue("Warning (No Html doc node) in RunHtmlAgilityPackThread -- " + url);
                        return;
                }
            }
            catch (WebException wx)
            {
                    _loggerQueue.Enqueue("Error in RunHtmlAgilityPackThread -- " + (wx.InnerException == null ? "" : wx.InnerException.Message) + " url --" + url);
            }
            catch (Exception ex)
            {
                    _loggerQueue.Enqueue("Error in RunHtmlAgilityPackThread -- " + (ex.InnerException == null ? "" : ex.InnerException.Message) + " url --" + url);
            }
        }


        private void GetImageUrls(IList<string> imgs, string url){
                    string url1, path;

                    try
                    {
                        for (int i = 0; i < imgs.Count; i++)
                        {
                            url1 = imgs[i].Replace("&amp;", "&");

                            if (url1.ToLower().Contains("image1") || url1.ToLower().Contains("image2"))
                            {
                                lock (objLocker)
                                {
                                    path = GetPath(url1, url);
                                    if (url1 != null)
                                    {
                                        _downloadImageUrls.Enqueue(url1);
                                        if (path != null)
                                            dc.Add(url1, path);
                                    }
                                }
                                DownloadFile();

                            }
                        }
                    }
                    catch (Exception ex)
                    {
                            _loggerQueue.Enqueue("Error in GetImageUrls -- " + (ex.InnerException == null ? "" : ex.InnerException.Message));
                    }
            }

        public void DownloadFile()
        {
            if (_downloadImageUrls.Any())
            {
                try
                {
                            WebClient imageclient = new WebClient();
                            imageclient.DownloadFileCompleted += client_DownloadFileCompleted;

                            var url = _downloadImageUrls.Dequeue();
                            string FileName = dc[url];

                            CreateDirectory(Path.GetDirectoryName(FileName));
                            imageclient.DownloadFileAsync(new Uri(url), FileName, FileName + " " + url);
                }
                catch (Exception ex)
                {
                        _loggerQueue.Enqueue("Error in DownloadFile -- " + (ex.InnerException == null ? "" : ex.InnerException.Message));
                }
                return;
            }

            // End of the download
            return;
        }

        private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            string filenameurl = string.Empty;

            try
            {
                filenameurl = (string)e.UserState;

                _loggerQueue.Enqueue(filenameurl);

                if (e.Error != null)
                {
                    _loggerQueue.Enqueue("Error in client_DownloadFileCompleted -- " + (e.Error.InnerException == null ? "" : e.Error.InnerException.Message));
                }
                if (e.Cancelled)
                {
                    // handle cancelled scenario
                }
            }
            catch (Exception ex)
            {
            }

            try
            {
                while(_loggerQueue.Count>0)
                    tw2.WriteLine(_loggerQueue.Dequeue());

                DownloadFile();
            }
            catch (Exception ex)
            {
                _loggerQueue.Enqueue("Error in tw2.WriteLine -- " + (ex.InnerException == null? "" : ex.InnerException.Message));
            }
            //mre.Set();
        }


        private string GetPath(string url, string origUrl)
        {
            string strDestRoot = (string)ConfigurationManager.GetAppSetting("DestRoot");
            StringBuilder sb = new StringBuilder(strDestRoot);
            StringBuilder sbFile = new StringBuilder();
            string strTableOrChart;
            string strConcept;
            string strSTCode;
            string strCYCode;
            string strGridCount;
            string strMSCode;

            try
            {
                string[] strParam0 = origUrl.Replace("&amp;", "&").Replace("&quot;", "").Split(new string[] { "?", "_Type=", "&Parameter1=", "&Parameter2=", "&" }, StringSplitOptions.RemoveEmptyEntries);

                string[] strParam = url.Replace("&amp;", "&").Replace("&quot;", "").Split(new string[] { "?", "_Type=", "&Parameter1=", "&Parameter2=", "&" }, StringSplitOptions.RemoveEmptyEntries);

                strTableOrChart = strParam[1];
                sb.Append(@"\");
                sb.Append(strTableOrChart);

                strConcept = strParam0[2];
                sb.Append(@"\");
                sb.Append(strConcept);
                CreateDirectory(sb.ToString());
                sb.Append(@"\");
                sb.Append(strConcept);
                strSTCode = strParam[4];


                if (url.Contains("ImageCYTables?Table_Type=") || url.Contains("CYImage?Chart_Type="))
                {
                    strCYCode = strParam[5].Replace("Selected_CYCde=", "");
                    sb.Append(strCYCode);
                }
                else if (url.Contains("MSImage?Chart_Type=") || url.Contains("ImageMSTable?Table_Type="))
                {
                    strMSCode = strParam[5].Replace("Selected_MSCde=", "");
                    sb.Append(strMSCode);
                }
                else if (url.Contains("ImageTableMGrids?Table_Type="))
                {
                    if (strParam.Length > 5)
                    {
                        strGridCount = strParam[5].Replace("Parameter3=", "_");
                        sb.Append(strGridCount);
                    }
                    else
                        sb.Append(strSTCode);
                }
                else if (url.Contains("ImageCYTable_MGrids?Table_Type="))
                {
                    strCYCode = strParam[5].Replace("Selected_CYCde=", "");
                    strGridCount = strParam[6].Replace("Parameter3=", "_");
                    sb.Append(strCYCode);
                    sb.Append(strGridCount);
                }
                else
                    sb.Append(strSTCode);

                sb.Append(".png");
                return sb.ToString().Replace("\"", "");
            }
            catch (Exception ex)
            {
                //lock (tw2)
                {
                    _loggerQueue.Enqueue("Error in getpath -- " + (ex.InnerException == null ? "" : ex.InnerException.Message));
                }
                return null;
            }
        }

        private bool CreateDirectory(string path)
        {
            lock (objLocker)
            {
                if (!Directory.Exists(path))
                {
                    // Try to create the directory.
                    DirectoryInfo di = Directory.CreateDirectory(path);
                    return false;
                }
                else
                    return true;
            }
        }
    }
}
                var doImages = GetDownLoadImagesThread();

                foreach (var aURL in iAllUrls.Distinct())
                {
                    doImages.Value.RunHtmlAgilityPackThread(aURL.Replace("&amp;", "&").Replace("\"",""));
                }


private ThreadLocal<DownLoadImages> GetDownLoadImagesThread()
{
    TextWriter tw2 = new StreamWriter(CreateURLFile(2));
    tw2 = TextWriter.Synchronized(tw2);

    return new ThreadLocal<DownLoadImages>(() => new DownLoadImages(tw2));
}