C# 错误处理system.outofmemoryexception
我正在使用多线程将内容发布到1个url。我的错误是,当我运行300个线程时,在10到15分钟后,我将被推出并得到消息“system.outofmemoryexception”。据我所知,我电脑的配置是:Ram 32G,12核,24线程。。。!我是新来的,所以我不知道如何处理它。你能帮助我吗?无论如何,我要感谢你 代码:C# 错误处理system.outofmemoryexception,c#,C#,我正在使用多线程将内容发布到1个url。我的错误是,当我运行300个线程时,在10到15分钟后,我将被推出并得到消息“system.outofmemoryexception”。据我所知,我电脑的配置是:Ram 32G,12核,24线程。。。!我是新来的,所以我不知道如何处理它。你能帮助我吗?无论如何,我要感谢你 代码: int-sothread=100; for(int ii=0;ii{ while(true){ 字符串url=”https://phamnhi.net"; string dat
int-sothread=100;
for(int ii=0;ii{
while(true){
字符串url=”https://phamnhi.net";
string data=“id=123213&view=333”;
html=PostData(http,url,data,“application/x-www-form-urlencoded;charset=UTF-8”).ToString();
试试{
如果(html.Contains(“OK”){
行。单元格[状态]。Value=“确定…”;
睡眠(20000);
http.Close();
}
}catch(OverflowException){break;}
}
});
t、 IsBackground=true;
t、 Start();
睡眠(1000);
}
听起来您需要在程序资源执行后释放它们。如果每个线程都在没有完成的情况下增加了更多的工作,那么最终您的内存将耗尽。
请发布一些代码进行分析。
int-sothread=100;对于(intii=0;ii{while(true){stringurl=”https://phamnhi.net“string data=“id=123213&view=333”;html=PostData(http,url,data,“application/x-www-form-urlencoded;charset=UTF-8”).ToString();try{if(html.Contains(“OK”){row.Cells[状态].Value=“ok…”Thread.Sleep(20000);http.Close();}}}catch(OverflowException){break;}}};t、 IsBackground=true;t、 Start();线程。睡眠(1000);}代码>@Navila:将代码编辑到问题中。不要发表评论!我编辑了这篇文章以包含评论中的代码。PostData
中的内容是什么?为什么http.com会在20秒后关闭?(睡眠(20000)=20秒)在我看来,当这些循环都完成时,您应该在这些循环之外的某个地方关闭http。请记住,这发生在100个循环中的无限循环中。
int sothread = 100;
for (int ii = 0; ii < sothread; ii++)
Thread t2 = new Thread(() => {
while (true) {
string url = "https://phamnhi.net";
string data = "id=123213&view=333";
html = PostData(http, url, data, "application/x-www-form-urlencoded; charset=UTF-8").ToString();
try {
if (html.Contains("OK")) {
row.Cells[status].Value = "ok...";
Thread.Sleep(20000);
http.Close();
}
} catch (OverflowException) { break; }
}
});
t.IsBackground = true;
t.Start();
Thread.Sleep(1000);
}