简体   繁体   English

c#application,任务管理器中的句柄增加和系统挂起

[英]c# application, Handles in task manager increase and system hanged

In our application we used Threads and Delegate to get the best performance. 在我们的应用程序中,我们使用Threads和Delegate来获得最佳性能。 In order to gate our operations we use the System.Timers.Timer class. 为了操作我们的操作,我们使用System.Timers.Timer类。 When the application start , after 1 hour the task manager shows that the number of handles increased and cpu usage increases as well. 当应用程序启动时,1小时后,任务管理器显示句柄数量增加,cpu使用量也增加。

What can we do to dispose objects of Thread and delegate? 我们可以做些什么来处置Thread和委托的对象?

below code can help you to check this task. 下面的代码可以帮助您检查此任务。

public partial class Form1 : Form
{
    System.Timers.Timer MainTimer;
    public Form1()
    {
        InitializeComponent();
        MainTimer = new System.Timers.Timer();
        MainTimer.Elapsed+=new System.Timers.ElapsedEventHandler(MainTimer_Elapsed);

    }
    Thread MainThread;
    private void button1_Click(object sender, EventArgs e)
    {
        MainTimer.Interval = 10;
        MainTimer.Start();
    }

    void MainTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        MainThread = new Thread(new ThreadStart(DoStart));
        MainThread.Start();
    }
    void DoStart()
    {
        PrintInfo();
    }
    delegate void PrintInfo_Delegate();
    void PrintInfo()
    {
        if (textBox1.InvokeRequired)
        {
            Invoke(new PrintInfo_Delegate(PrintInfo));
        }
        else
        {
            textBox1.Text += "Test\r\n";
        }
    }
}

Apologies if I'm misparsing something, but it looks like once the button is clicked, you're then starting a new thread (in MainTimer_Elapsed) every 10 milliseconds? 抱歉,如果我误解了一些东西,但看起来一旦点击按钮,你就会每10毫秒开始一个新线程(在MainTimer_Elapsed中)? Without knowing more about the context / versions, it's hard to give specific advice on an alternative (async/await, TPL, etc), but off-hand it seems like anything creating new threads (especially non-background threads) like that is doing so unnessarily? 在不了解更多有关上下文/版本的情况下,很难就替代方案(async / await,TPL等)提供具体的建议,但是现在看起来就像创建新线程(尤其是非后台线程)一样如此恍惚?

For processing non-UI work, perhaps use ThreadPool.QueueUserWorkItem instead of creating your own? 对于处理非UI工作,也许使用ThreadPool.QueueUserWorkItem而不是创建自己的? For UI work, maybe use BackgroundWorker? 对于UI工作,可能使用BackgroundWorker? If you're exhausting the thread pool, you could also consider setting things like max thread count, but using TPL and avoiding the use/creation/management of threads would be better/simpler IMHO if you're able to target .Net 4.x 如果你正在耗费线程池,你也可以考虑设置最大线程数,但是如果你能够以.Net 4为目标,那么使用TPL并避免使用/创建/管理线程会更好/更简单。 X

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

相关问题 通过任务管理器或系统重启杀死应用程序时,如何优雅地退出C#应用程序 - How to exit C# application gracefully when application is killed through task manager or system restart 像任务管理器一样在c#中切换应用程序 - Switch application in c# like task manager 为什么Visual C#express 2010变得缓慢而且不稳定,并且任务管理器中的句柄变得过高? - Why does Visual C# express 2010, becomes slow and choppy, and the handles in Task manager become excessively high? C#系统CPU使用率并与Windows任务管理器同步 - C# System CPU Usage and syncing with Windows Task Manager 如何在给定情况下了解调试/配置文件挂起的C#应用​​程序 - How to know debug/profile hanged C# application in the given situation C#应用程序退出后在任务管理器中仍然可见 - C# application It still visible in Task Manager after exit C# 我的应用程序在任务管理器中启动但未显示在桌面中 - C# my application starts in task manager but not shown in Desktop 在C#中使用句柄和System watcher - Using handles and System watcher in c# C#应用程序未在telnet中显示窗口句柄 - C# application not showing window handles in telnet 使用Task C#进行系统破解 - System crack with Task C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM