Results 1 to 4 of 4

Thread: [RESOLVED] CPU usage in network app (threading?)

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    45

    Resolved [RESOLVED] CPU usage in network app (threading?)

    Hi guys,
    Ok so I'm writing a chat application like thing, which will actually be used in a similar way to MSN Messenger's Whiteboard function.

    My problem is that as soon as I start the 'server' program (which also includes a client!) the CPU usage shoots up to 100% and stays there. I think it might be because there is a thread in the background listening for new connections. Is there any way to reduce the CPU usage? (Its slowing down the rest of the app, and don't even think about trying to do anything else!) I've tried reducing the thread priority, but that just seemed to make client's connection take an age.

    I can post some source code if you'd like...

    Thanks, Mech
    VB.2003

  2. #2
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: CPU usage in network app (threading?)

    how are you listening? are you using a loop?
    Chris

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    45

    Re: CPU usage in network app (threading?)

    Ok, I'm using my network as an attached class, the relevant code is as follows:

    VB Code:
    1. Public Sub New(Optional ByVal address As String = "localhost", Optional ByVal port As Integer = 5555)
    2.  
    3.       Try
    4.             ' Start listening for client requests in the background
    5.             LC = New ListenClass(globalAddress, globalPort)
    6.             Dim TSThread As Thread
    7.             TSThread = New Thread(AddressOf LC.StartListening)
    8.             TSThread.Priority = ThreadPriority.BelowNormal
    9.             TSThread.Start()
    10.          Catch .....

    So thats when you create a new instance of the server class (called LC) the LC.StartListening Sub looks like this:

    VB Code:
    1. Try
    2.                 If localListener Is Nothing Then
    3.                     localListener = New TcpListener(Dns.Resolve(ipHost).AddressList(0), ipPort)
    4.                 End If
    5.                 localListener.Start()
    6.                 tClient = localListener.AcceptTcpClient
    7.                 RaiseEvent Connected(tClient)
    8.             Catch .......

    The connected event saves the TCP client information and starts a new Thread to listen for more connections.

    Actually, just had a thought... Should I be stopping or disposing of 'TSThread' in the connect code or will that free the memory automatically once a client is connected?

    Hope this is not too indigestible! Any help would be greatly appreciated.
    VB.2003

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    45

    Re: [RESOLVED] CPU usage in network app (threading?)

    The solution to my problem is here:
    http://www.vbforums.com/showthread.p...&is_resolved=1
    VB.2003

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width