473,394 Members | 1,869 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,394 software developers and data experts.

infinite NetworkStream (CPU consuming)

psg
Hello!

I am developing Windows application which after sending a request to a
server will listen to response, a response will be infinite in length, but
will not be continuous in time.

What I have done is in a back thread I prepared infinite loop and there I am
checking for new packages. Its scheme looks like this:

***********************************
while(shallRun)
{
loopCounter++;

while(networkStream.DataAvailable)
{
networkStream.Read(buffer, 0, buffer.Length);

packagesCounter++;

//do some data manipulation here
//UpdateUI
}
}
***********************************

Unfortunatelly for every package my program is doing thousands or even
million "empty" loops. Probably that is why CPU is very high, and can even
get to 100%.

Do you know some other C# solution for reading infinite NetworkStream. Some
event OnNewData? or maybe some other solutions.

TIA

RGDS PSG

--
Nothing happens until somebody sells something.
Nic sie nie dzieje, poki ktos komus czegos nie sprzeda.
http://kickme.to/psg

Nov 16 '05 #1
2 3939
psg,

I would use the BeginRead and EndRead methods to do this. The
information that is coming to you is in groups of bytes that compose a
message, and you are performing some action based on that message. When you
start listening, you
call BeginRead, trying to read the message from the socket.

In the event handler for the callback to BeginRead, you can get the
message. If you require more bytes for the message, then read as much as
you need for that particular message. Process the message, and then kick
off another call to BeginRead at the end, starting it all over again. This
way, your callback will be notified when you have a new message to process,
and it will place itself in a state to receive the next one.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"psg" <pg******@ki.net.pl> wrote in message
news:c8**********@b177.ki.net.pl...
Hello!

I am developing Windows application which after sending a request to a
server will listen to response, a response will be infinite in length, but
will not be continuous in time.

What I have done is in a back thread I prepared infinite loop and there I am checking for new packages. Its scheme looks like this:

***********************************
while(shallRun)
{
loopCounter++;

while(networkStream.DataAvailable)
{
networkStream.Read(buffer, 0, buffer.Length);

packagesCounter++;

//do some data manipulation here
//UpdateUI
}
}
***********************************

Unfortunatelly for every package my program is doing thousands or even
million "empty" loops. Probably that is why CPU is very high, and can even
get to 100%.

Do you know some other C# solution for reading infinite NetworkStream. Some event OnNewData? or maybe some other solutions.

TIA

RGDS PSG

--
Nothing happens until somebody sells something.
Nic sie nie dzieje, poki ktos komus czegos nie sprzeda.
http://kickme.to/psg


Nov 16 '05 #2
psg
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
I would use the BeginRead and EndRead methods to do this.


I thought that it would be too hard to do but finally I got it right.
Frankly speaking now I combined two methods BeginRead and Read. When waiting
for new message (seeking for new message beginning) I use BeginRead, when
processing message and working with known length of packages I use Read, but
now it doesn't use CPU at all, because with your help I got rid off that
erroneous "empty" loop. THX!

RGDS PSG

Nov 16 '05 #3

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

Similar topics

2
by: vishal | last post by:
Hi I a am executing a stored procedure from C code using SQLExecute . It takes some time to Execute the procedure. However when I see the processor usage during that time it shows 100% cpu usage....
7
by: Andrej Hocevar | last post by:
Hello, what is the best way of solving cases where one has to wait (forever, until killed or instructed otherwise) for some event to occur? E.g., wait for data, monitoring a file? In fact, that...
1
by: Jim P. | last post by:
I'm having trouble returning an object from an AsyncCallback called inside a threaded infinite loop. I'm working on a Peer2Peer app that uses an AsyncCallback to rerieve the data from the remote...
9
by: Droopy | last post by:
Hi, When I start my application, I notice that it takes 50% CPU in idle status ! I used Process Explorer from Sysinternals to find what thread is consuming that much CPU. Only 1 thread is...
2
by: Glenn Thomson | last post by:
Hello: Use C# 2003 .Net Frameworks 1.1 SP1, OleDbConnection I open an Access data using a class (dataset has many pictures in it) and scroll back an forth using the four arrow keys. Recs displayed...
2
by: PiotrKolodziej | last post by:
Hi I have a simple question. Here is the code related to my question: while (true) { if (tcpListenerServer.Pending() && !this.Disposing) { TcpClient tcpClient =...
10
by: =?ISO-8859-1?Q?G=E9rard_Talbot?= | last post by:
www.authoring.stylesheets] Dear fellow CSS colleagues and web authors in alt.html discussion forum, I would like to ask you to help me confirm that there is a serious bug in IE 7 final release...
0
by: etnaelk | last post by:
Hi all, I have a real bugger of a problem that I just haven't been able to figure out. I am working on writing my own proxy server in C# using TcpListener, TcpClient, HttpWebRequest/Response and...
12
by: sethukr | last post by:
Hi everybody, Can anyone give me a simple program which would consumes more CPU usage (more than 50%) in windows? Thanks, Sethu
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.