Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.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#_Mono_Threadpool_Webclient - Fatal编程技术网

C# WebClient下载字符串在线程池中不工作

C# WebClient下载字符串在线程池中不工作,c#,mono,threadpool,webclient,C#,Mono,Threadpool,Webclient,我目前有一个项目,我尝试使用C#控制台应用程序从NZBIndex.nl API获取信息 我的请求如下所示: Thread was being aborted. at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_threads_state_poll() at System.Uri.GetComponentsHelper (System.UriComponents uriComponents, System.U

我目前有一个项目,我尝试使用C#控制台应用程序从NZBIndex.nl API获取信息

我的请求如下所示:

Thread was being aborted.
  at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_threads_state_poll()
  at System.Uri.GetComponentsHelper (System.UriComponents uriComponents, System.UriFormat uriFormat) <0x40a23510 + 0x00033> in <4e15bbae9d7043d8afd6cfd50bd9bd5a>:0 
  at System.Uri.GetComponents (System.UriComponents components, System.UriFormat format) [0x00072] in <4e15bbae9d7043d8afd6cfd50bd9bd5a>:0 
  at System.Uri.GetParts (System.UriComponents uriParts, System.UriFormat formatAs) [0x00000] in <4e15bbae9d7043d8afd6cfd50bd9bd5a>:0 
  at System.Uri.get_Query () [0x00041] in <4e15bbae9d7043d8afd6cfd50bd9bd5a>:0 
  at System.Net.WebClient.GetUri (System.Uri address) [0x00035] in <4e15bbae9d7043d8afd6cfd50bd9bd5a>:0 
  at System.Net.WebClient.GetUri (System.String address) [0x0004c] in <4e15bbae9d7043d8afd6cfd50bd9bd5a>:0 
  at System.Net.WebClient.DownloadString (System.String address) [0x00000] in <4e15bbae9d7043d8afd6cfd50bd9bd5a>:0 
  at (wrapper remoting-invoke-with-check) System.Net.WebClient.DownloadString(string)
  at DBErrorcheck.Program.CheckThread (System.Object o) [0x00039] in <cf4c719a35df4ff094732dd5a9e883e9>:0 
使用(WebClient c=new WebClient())
{
var response=c.DownloadString(“https://nzbindex.com/search/json?sort=agedesc&hidespam=1&q=Ubuntu");
控制台写入线(响应);
//...
}
在我的主函数中以及在手动创建的线程中执行它时,这种方法工作得很好。但是,当运行线程池调用的函数中截取的代码时,它会抛出错误,如下所示:
ThreadPool.QueueUserWorkItem(检查方法,项)

使用ThreadPool运行时引发的错误如下所示:

Thread was being aborted.
  at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_threads_state_poll()
  at System.Uri.GetComponentsHelper (System.UriComponents uriComponents, System.UriFormat uriFormat) <0x40a23510 + 0x00033> in <4e15bbae9d7043d8afd6cfd50bd9bd5a>:0 
  at System.Uri.GetComponents (System.UriComponents components, System.UriFormat format) [0x00072] in <4e15bbae9d7043d8afd6cfd50bd9bd5a>:0 
  at System.Uri.GetParts (System.UriComponents uriParts, System.UriFormat formatAs) [0x00000] in <4e15bbae9d7043d8afd6cfd50bd9bd5a>:0 
  at System.Uri.get_Query () [0x00041] in <4e15bbae9d7043d8afd6cfd50bd9bd5a>:0 
  at System.Net.WebClient.GetUri (System.Uri address) [0x00035] in <4e15bbae9d7043d8afd6cfd50bd9bd5a>:0 
  at System.Net.WebClient.GetUri (System.String address) [0x0004c] in <4e15bbae9d7043d8afd6cfd50bd9bd5a>:0 
  at System.Net.WebClient.DownloadString (System.String address) [0x00000] in <4e15bbae9d7043d8afd6cfd50bd9bd5a>:0 
  at (wrapper remoting-invoke-with-check) System.Net.WebClient.DownloadString(string)
  at DBErrorcheck.Program.CheckThread (System.Object o) [0x00039] in <cf4c719a35df4ff094732dd5a9e883e9>:0 
线程正在中止。
at(wrapper managed to native)System.Object.\uuIcall\uwrapper\uMono\uThreads\uState\uPoll()
位于0中的System.Uri.GetComponentsHelper(System.UriComponents,System.UriFormat UriFormat)
在:0中的System.Uri.GetComponents(System.UriComponents,System.UriFormat格式)[0x00072]处
位于:0中的System.Uri.GetParts(System.uriParts、System.UriFormat formatAs)[0x00000]处
在:0中的System.Uri.get_查询()[0x00041]处
位于:0中的System.Net.WebClient.GetUri(System.Uri地址)[0x00035]处
位于:0中的System.Net.WebClient.GetUri(System.String地址)[0x0004c]处
位于:0中的System.Net.WebClient.DownloadString(System.String地址)[0x00000]处
at(包装器远程调用,带检查)System.Net.WebClient.DownloadString(字符串)
在0中的DBErrorcheck.Program.CheckThread(System.Object o)[0x00039]处

调试它并没有真正的帮助,因为程序在执行下载字符串后停止了

我只是找到了这个问题的答案,同时尝试制作一个最小的可复制示例

当线程池中的线程仍在运行但主线程已退出时,可能会导致此错误。我发现这一点是因为使用
线程.Sleep(500)
为我修复了它


我想更好的方法是检查池是否还有剩余的工作。

请共享一个。或者简单地捕获并忽略此类异常,因为您已经知道它们是无害的。