I am deving an application for smartphone. It sends and recieves data from a server computer (also made in C#)
The thing is, I have a while loop going on in order to check for incoming data and it can slow down the application a fair amount
Is there a better way to check for incoming data without the need for a while loop going on continuesly
void Recieve(){ while (!this.readData && this.ns.CanRead) { if(this.ns.DataAvailable){ byte[] incomingData = new byte[64]; int theData = ns.Read(incomingData, 0, incomingData.Length); this.HandleIncomingData(incomingData); this.readData = false;} } } |
This method is running on a seperate thread and makes no difference when not running it on a seperate thread.
the "HandleIncomingData" basically checks for the incoming byte and I do actions according to it.
Wierd thing is, when the data I want has been recieved, it doesnt do anything apart from locking up the application (no exceptions)
however if i set a breakpoint and debug the application, everything is fine and does what I want it to do (display data)
So, 2 Q's.... 1 about performance and the 2nd is why it doesnt do what I tell it to do unless I hit a breakpoint and run it from there...
Any advice is much appreciated

speed up app?
iestudi
Cheers
Daniel
greekTowner
Have you tried it
You haven't shown enough code but I guess your "ns" object is a NetworkStream. It has BeginRead method (amongst many others):
http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemnetsocketsnetworkstreammemberstopic.asp
The documentation (although not perfect) indicates when a method is "Supported by the .NET Compact Framework."
This example was written with full framework in mind but the same principles apply:
http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpconUsingNon-blockingClientSocket.asp
If you are still having problems post the code you have tried and exactly where the problem is.
Cheers
Daniel
Elisabeth Boonin
elfranko
I found the reason why the app was slow. My fault really - amazing what happens when you only get 2 hours worth of sleep in the week!
I was creating a thread everytime the send button was clicked... and that thread executes a method which has a while loop....you see where I'm going :)
I think (unsure) maybe .NET CF 2.0 supports asynchronous stuff however unfortunatly no mobile devices in the UK support WM2005 which has .NET CF 2.0
XMLHacker
In case you decide to come back to this and do it properly, asynchronous reads are supported with v1 and if you try the sample I referred you to you'll work it out. On your specific comment on StateObject, search the page I linked to and you'll see that it is a class you define yourself. Again, if you try something and fail, post the code so we can help you.
On your other point on CF v2.0, note that it also supports Pocket PC 2003.
Cheers
Daniel