简体   繁体   English

减少线程CPU使用率

[英]Reduce thread CPU usage

So I have 2 threads in my application which work fine except for the fact that they will continue taking up rather large amounts of my processor even after they are closed or aborted. 因此,我的应用程序中有2个线程可以正常工作,除了它们即使在关闭或中止之后仍会继续占用大量处理器的事实。 This is how one of the loops look like along with some of the main declarations. 这就是其中一个循环以及一些主要声明的样子。

Dim Thread1 As System.Threading.Thread
Dim ThreadRunning As Boolean
Dim Thread1Running As Boolean = False

Sub Something()
    While True
        If ThreadRunning = True Then
            Try
                ...Actions which don't necessarily affect the thread
            Catch ex As Exception
            End Try
        ElseIf ThreadRunning = False Then
            If Thread1Running = True Then
                Thread1Running = False
                Thread1.Abort()
            Else
            End If
        End If
    End While
End Sub

Here is the code I used to start the thread. 这是我用来启动线程的代码。

Thread1Running = True
Dim Thread1 As New System.Threading.Thread(AddressOf Something)
Thread1.IsBackground = True
Thread1.Priority = ThreadPriority.Lowest
Thread1.Start()

Here is the code I use to stop the thread. 这是我用来停止线程的代码。

ThreadRunning = False

The actions in the thread need threads since timers were too slow for the task(even at a 1 ms interval). 线程中的操作需要线程,因为计时器对于任务而言太慢(即使间隔为1 ms)。 The actions are performed fine except for when I abort the thread. 除了我中止线程时,这些动作执行得很好。 In this case the thread will close and the CPU usage will go from around 25% to 0% for this program but then it will crash with a CLR error. 在这种情况下,该程序线程将关闭并且该程序的CPU使用率将从25%变为0%,但随后它会由于CLR错误而崩溃。 When I aborted the thread from outside the sub it would still leave the program CPU usage at 25% which is what I'm trying to avoid. 当我从子外部中止线程时,它仍然会使程序CPU使用率保持在25%的水平,这是我要避免的事情。 So my main question is: Is it possible to close the thread and reopen it later without it crashing, and so that while it is closed it won't use up 25% of the CPU(or some other rather high CPU performance)? 所以我的主要问题是:是否可以关闭线程并在以后重新打开它而不会导致它崩溃,以便在关闭该线程时不会占用25%的CPU(或其他相当高的CPU性能)? If this isn't enough information I will provide more but I thing this will suffice... hopefully. 如果这还不够,我将提供更多信息,但我希望这足够了……希望如此。 Thanks for any help. 谢谢你的帮助。

Here's the solution I found which worked. 这是我发现有效的解决方案。

While True
        If ThreadRunning = True Then
            Try
                ...Code stuff
            Catch ex As Exception
            End Try
        ElseIf ThreadRunning = False Then
            Exit While
        End If
    End While
Exit Sub

Apparently checking it this way made it so that 1. The thread doesn't hang. 显然,通过这种方式进行了检查,从而使其1.线程未挂起。 2. It fully exits the thread and prevents it from using the CPU while closed. 2.它完全退出线程,并在关闭时阻止其使用CPU。 And 3. It allows for reusing the threads through the same method as shown above. 和3.它允许通过与上述相同的方法重用线程。

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

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