Reduce CPU usage

  • Thread starter Thread starter Lespaul36
  • Start date Start date
L

Lespaul36

I am working on a chat program. I have a socket set up to listen, opens a
new thread and loops waiting for a connection. Problem is that it is using
like 98% of my CPU...any ideas on a way that will still be effective, but
won't bog down the system?
 
I am working on a chat program. I have a socket set up to listen, opens a
new thread and loops waiting for a connection. Problem is that it is using
like 98% of my CPU...any ideas on a way that will still be effective, but
won't bog down the system?

Yes, use asnyc sockets - then you don't have to loop. Or if you don't
want to do that - then put a sleep in the loop
(system.threading.thread.sleep).

If you decide to go with an async socket (which is really the best way)
then here is a link on .NET sockets - both syncronous and asyncronous...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconsockets.asp

HTH
 
An alternative solution even to Tom's suggestion is to use Web Services.
This will of course only work if you are doing peer to peer chat. If you
want to build your own IRC-like program then this solution would require
that a central server keep track of who it needs to speak to.

I seem to recall that Carl Franklin (www.franklins.net) wrote a piece about
how to write a secure web-services chat program in MSDN magazine. Might be
worth searching the magazine archives at msdn.microsoft.com.

Good luck
 
I have tried many things. I still have not found anything that seems to
work. Here is the portions of my code that deal with the socket maybe you
have a better idea? The form still seems to hang. I got it to not hang if
I used a loop with a application.doevents, but it was not CPU friendly.

'Declarations
Private SocketChatS As Socket
Private SocketChatC As TcpClient
Private marrClients As New ArrayList
Private allDone As New System.Threading.ManualResetEvent(True)



Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
If Not designmode Then
Dim ipAdd() As System.Net.IPAddress =
System.Net.Dns.Resolve("localhost").AddressList
mthrListen = New System.Threading.Thread(AddressOf DoListen)
mthrListen.Start()
End If
End Sub

Private Sub DoListen()
Dim Iphe As IPHostEntry = Dns.Resolve(Dns.GetHostName)
Dim Ipep As New IPEndPoint(IPAddress.Any, MYPORT)
SocketChatS = New Socket(Ipep.Address.AddressFamily, SocketType.Stream,
ProtocolType.Tcp)
Dim blnFirst As Boolean = True
Dim pobjFRM As frmServerChatUI

With SocketChatS
.Blocking = False
.Bind(Ipep)
.Listen(100)
End With

While True
allDone.Reset()
SocketChatS.BeginAccept(New AsyncCallback(AddressOf AcceptClient),
SocketChatS)
allDone.WaitOne()
End While

End Sub

Private Sub AcceptClient(ByVal ar As IAsyncResult)
allDone.Set()

Dim pobjFRM As New frmServerChatUI
pobjFRM.Show()

Dim pintIndex As Integer = marrClients.Add(New HabibSocket.HabibsSocket)
marrClients(pintIndex).tcpsocket = SocketChatS.EndAccept(ar)

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top