John
John

Reputation: 785

vb.net - service app & forms app both have very high CPU usage since adding socket comms

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

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.

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.

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.

Any help would be greatly appriciated!

Upvotes: 0

Views: 206

Answers (1)

Craig
Craig

Reputation: 2494

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:

  1. Call StartListener from other code.
  2. StartListener creates a new thread, with StartListener as the startup routine.
  3. StartListener starts the new thread.
  4. The new thread starts running and runs StartListener.
  5. Cycle back to (2)

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.

Upvotes: 1

Related Questions