473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reduce CPU usage

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?
Nov 21 '05 #1
3 1901
On 2005-02-08, Lespaul36 <le*******@none.net> wrote:
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/de...consockets.asp

HTH
--
Tom Shelton [MVP]
Nov 21 '05 #2
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
--
Pete Wright
Author of ADO.NET Novice to Pro for Apress
www.petewright.org

"Lespaul36" <le*******@none.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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?

Nov 21 '05 #3
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

"Tom Shelton" <ts******@YOUKNOWTHEDRILLcomcast.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
On 2005-02-08, Lespaul36 <le*******@none.net> wrote:
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/de...consockets.asp

HTH
--
Tom Shelton [MVP]

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
0
by: Jéjé | last post by:
Hi, I'm looking for articles or sample code to reduce the viewstate usage in ASP.NET 2 I want to use the treeview + gridview to create a hierarchical display of my data. The grid(s) can be...
0
by: Boris Borcic | last post by:
On the python3000 mailing list there was some discussion of a "comprehension syntax" for reduce. This inspired me the following proof-of-concept in pure python 2.5a1, although I don't know if it...
2
by: goRide | last post by:
Hi, i have a small application setting in tray. The main form is hidden but has a media player component on it which plays a url link (radio tuner...). Anyway, my application take about 35MB of...
37
by: Ajai Jose | last post by:
Hi , I work on an ARM processor based embedded system. Code is mainly in C language. The project has a huge source base. We are supposed to optimise it. Most datastructures are declared as static...
7
by: cnb | last post by:
This must be because of implementation right? Shouldn't reduce be faster since it iterates once over the list? doesnt sum first construct the list then sum it? ----------------------- reduce...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.