vb.net - 服务应用程序自从添加套接字通信以来,表单应用程序的 CPU 使用率都非常高 [英] vb.net - service app & forms app both have very high CPU usage since adding socket comms
问题描述
我正在开发 2 个应用程序 - 一个是 Windows 服务,它执行后台任务和 API 调用等.第二个是在用户上下文中运行的表单应用程序.表单应用程序的目的是在 Windows 服务需要时向用户显示数据.
I've got 2 apps I'm working on - one is a windows service which does background tasks and api calls etc. The second is a forms app that runs in the user context. The purpose of the forms app is to display data to the user as and when required by the windows service.
为了启用两个应用程序之间的通信,我使用了线程和套接字.我以前没有这样做过,所以如果我犯了任何明显的错误,请放轻松.两个应用程序中的代码大致如下,除了每个应用程序的端口号不同......
To enable the comms between the two apps, I used threading and sockets. I've not done this before, so please go easy if I have made any glaring mistakes. Code is pretty much as follows in both apps, except the port number which is different for each...
Imports System.Net.Sockets
Imports System.Threading
Public Listener As New TcpListener(64420)
Public Client As New TcpClient
Public Message As String = ""
Public Function StartListener()
Try
Dim ListenerThread As New Thread(New ThreadStart(AddressOf StartListener))
ListenerThread.Start()
Listener.Start()
Timer1.Start()
Catch ex As Exception
End Try
End Function
Public Function MessageClient(ByVal Message As String)
Try
Client = New TcpClient("127.0.0.1", 64421)
Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write(Message)
Writer.Flush()
Return "OK"
Catch ex As Exception
Return "Error"
End Try
End Function
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Listener.Pending = True Then
Message = ""
Client = Listener.AcceptTcpClient()
Dim Reader As New StreamReader(Client.GetStream())
While Reader.Peek > -1
Message = Message + Convert.ToChar(Reader.Read()).ToString
End While
ResponseCalc(Message)
End If
End Sub
两个应用程序之间的通信完美无缺,在这两种情况下,消息"都会传递给 ResponseCalc 函数,该函数确定需要在两端执行的操作.
The comms between the two apps works perfectly and in both instances, the 'message' is passed to the ResponseCalc function which determines what needs to be done at both ends.
问题是,自从把它放进去后,我的两个应用程序都使用了大量的 CPU.以至于我不断地以 100% CPU 使用我的机器.
The issue is that since putting this in, both my applications are using a crazy amount of CPU. To the point that I'm maxing my machine out at 100% CPU constantly.
有没有一种方法可以在没有相应的疯狂 CPU 使用率的情况下达到相同的结果?
Is there a way I can achieve the same result without the corresponding crazy CPU usage?
否则,您还能如何在此类应用之间启用通信?
Or failing that, how else might you go about enabling comms between apps like this?
最初我想在磁盘上写入/读取文件,但不想这样做,因为它感觉马虎和业余.
Originally I was thinking about writing/reading files to and from disk, but didn't want to be doing that as it felt sloppy and amateur.
任何帮助将不胜感激!
推荐答案
问题在于您的 StartListener
函数,该函数作为外部调用函数执行双重任务以启动侦听器线程和作为该线程的启动函数.这是在创建无限递归.执行顺序如下:
The problem is with your StartListener
function, which is doing double duty as an externally-called function to start up the listener thread and as the startup function for that thread. This is creating an infinite recursion. The execution sequence looks like this:
- 从其他代码调用
StartListener
. StartListener
创建一个新线程,使用StartListener
作为启动例程.StartListener
启动新线程.- 新线程开始运行并运行
StartListener
. - 循环回到 (2)
- Call
StartListener
from other code. StartListener
creates a new thread, withStartListener
as the startup routine.StartListener
starts the new thread.- The new thread starts running and runs
StartListener
. - Cycle back to (2)
要解决这个问题,启动例程应该不是StartListener
.我不确定您是否真的需要该线程执行除承载套接字处理的某些方面之外的任何其他操作;如果它只是需要存在,那么启动例程可以像一个空白的 Sub
一样简单.否则,您可以内联编写它,也可以将 AddressOf
与单独编写的例程一起使用.
To fix this, the startup routine should be something other than StartListener
. I'm not sure if you actually need the thread to do anything other than host some aspect of the socket handling; if it just needs to exist, then the startup routine could be as simple as a blank Sub
. Otherwise, you could write it inline or you could use AddressOf
with a separately-written routine.
或者,在另一种情况下,您似乎根本不需要编写自己的线程,正如您的评论所暗示的那样.
Or, in the alternative, it seems like you might not need to write your own thread at all, as your comment would suggest.
这篇关于vb.net - 服务应用程序自从添加套接字通信以来,表单应用程序的 CPU 使用率都非常高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!