gpt4 book ai didi

c# - ActiveX 对象和 .NET 垃圾收集器

转载 作者:太空宇宙 更新时间:2023-11-03 11:51:01 27 4
gpt4 key购买 nike

我在 .net 中遇到多线程问题。使用以下代码:

class Program
{
private static ManualResetEvent[] resetEvents;

private void cs(object o)
{
int xx = 0;
while (true)
{
xx++;
System.Xml.XmlDocument document = new System.Xml.XmlDocument();
document.Load("ConsoleApplication6.exe.config");

MSScriptControl.ScriptControlClass s =
newMSScriptControl.ScriptControlClass();
s.Language = "JScript";
object res = s.Eval("1+2");
Console.WriteLine("thread {0} execution {1}" , o , xx);
System.Threading.Thread.Sleep(5000);
}

}
static void Main(string[] args)
{
Program c = new Program();
for (int i = 0; i < 1000; i++)
{
System.Threading.Thread t = new System.Threading.Thread(
new System.Threading.ParameterizedThreadStart(c.cs));
t.Start((object)i);
}
}
}

当这段代码执行时,它会在几分钟后崩溃。为什么会崩溃?我可以做些什么来防止崩溃?

最佳答案

您正在启动 1000 个线程。仅堆栈空间就有 1000 MB 加上线程创建的所有对象。我的猜测是你的内存不足。你想要完成什么?

关于c# - ActiveX 对象和 .NET 垃圾收集器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2133423/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com