简体   繁体   English

是什么导致我的TPL任务停止运行?

[英]What can cause my TPL tasks to stop running?

I have the following code: 我有以下代码:

while (true)
{
    Task.Run(() =>{
        var request = WebRequest.Create("url1");
        request.GetResponse();
    });

    Task.Run(() =>{
        var request = WebRequest.Create("url2");
        request.GetResponse();
    });

    Thread.Sleep(15000);
}

The idea is every 15 seconds invoke a couple of endpoints. 这个想法是每15秒调用两个端点。 It works fine for around 20 mins then eventually the code in the tasks never run. 它可以正常运行约20分钟,然后最终任务中的代码将永远无法运行。 No errors. 没有错误。 Have I created too many tasks? 我创建了太多任务吗? There are no out of memory issues either. 也没有内存不足的问题。

从@AlexeiLevenkov给出的注释中得到提示,该注释从request.GetResponse获取对响应对象的引用,然后关闭响应以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM