本文介绍了我是否会产生手柄泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好, 我正在写一个proggi,它应该监视一些进程。在做这个的时候,我需要一个测试程序,然后编写一个,除了使用消耗一些cpu,有时候更多,有时更少,只需要一个测试程序。当我监视这个 进程一段时间后,我发现句柄不断增加。我假设 这是一个错误,但我不明白,这可能位于我的 简单应用程序中。核心如下: private void OnTimer(对象发送者,ElapsedEventArgs e)//每10次触发秒 { int steps = 0; this.counter ++; this.counter = this.counter%10; if(this.counter == 4) { steps = 1000; } else { if(this.counter == 9) { steps = 10000000 ; } } if(steps!= 0) { 工作j =新工作(步骤); ThreadStart ts = new ThreadStart(j.Run); 线程t =新线程(ts); t.IsBackground = true; t.Start(); } } 私人课程工作 { private int steps; public Job(int steps) {this.steps = steps; } public void Run() { Console.WriteLine(" Job.Run; Steps:{0} ",this.steps); //消耗一些cpu for(int i = 0; i< this.steps ; i ++); } } //类 此程序中线程数稳定[正如我所料],手柄 正在不断增加。我的错在哪里?这些手柄是什么? 一些帮助非常欢迎!! 祝你好运, Manfred Braun (私人) 曼海姆 德国 mailto:_m **** *********@manfbraun.de (删除反垃圾邮件下划线给我发邮件!) Hi All, I am writing a proggi, which should monitor some processes. While doingthis, I needed a test-program and wrote one, which does nothing else than toconsume some cpu, sometimes more, sometimes less. As I monitored thisprocess for a time, I found the handles increasing without end. I assumethis is a bug, but I do not understand, where this could be located in mysimple app. The core is as follows: private void OnTimer(object sender, ElapsedEventArgs e) //fires every 10seconds{int steps = 0;this.counter++;this.counter = this.counter % 10; if(this.counter == 4){steps = 1000;}else{if(this.counter == 9){steps = 10000000;}} if(steps != 0){Job j = new Job(steps);ThreadStart ts = new ThreadStart(j.Run);Thread t = new Thread(ts);t.IsBackground = true;t.Start();}} private class Job{private int steps; public Job(int steps){ this.steps = steps; } public void Run(){Console.WriteLine("Job.Run;Steps:{0}", this.steps); //Consume some cpu for(int i = 0; i < this.steps;i++);}}//class The number of threads is stable in this program [as I expect], the handlesare increasing endless. Where is my fault? And what are these handles?Some help would really be very welcomed!! Best regards,Manfred Braun (Private)MannheimGermany mailto:_m*************@manfbraun.de(Remove the anti-spam-underscore to mail me!)推荐答案 这篇关于我是否会产生手柄泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-21 09:48