NET CF Socket Gets Timeout

I am working on a Windows CE 5.0 application from .NET CF 3.5 SP1. I want to simulate a socket receiving timeout and write some codes:

... AutoResetEvent auto = new AutoResetEvent(false); mySocket.BeginReceiveFrom(arrData, 0, 4, SocketFlags.None, ref EP, new AsyncCallback(ReceiveCallback), mySocket); //if (auto.WaitOne(10000,false)) or : if (auto.WaitOne()) { // program flow never comes here, even after setting signal! _log.AppendLine("Message receive success"); } ... 

and here is my callback method:

 void ReceiveCallback(IAsyncResult ar) { bool b = ((EventWaitHandle)ar.AsyncWaitHandle).Set(); _log.AppendLine(string.Format("AsyncWaitHandle.Set() called and returned {0}",b)); } 

as I tested the application and registered some information, I immediately get the data, and ar.AsyncWaitHandle.Set () returns true, but why does the program flow never end? what's wrong?

+1
c # windows-ce compact-framework
source share
2 answers

I have to pass WaitHandle i, created, for example, as a StateObject parameter, to the BeginReceiveFrom method, which I can access later in the callback method. I edited my code and now it works. in fact, I think the corresponding patterns on the Internet are so weak and terrible.

 ... EventWaitHandle auto = new EventWaitHandle(false, EventResetMode.ManualReset); auto.Reset(); mySocket.BeginReceiveFrom(arrData, 0, 4, SocketFlags.None, ref EP, new AsyncCallback(ReceiveCallback), auto); if (auto.WaitOne(10000, false)) { _log.AppendLine("Message lenght receive success"); } ... 

and

 void ReceiveCallback(IAsyncResult ar) { bool b = ((EventWaitHandle)ar.AsyncState).Set(); _log.AppendLine(string.Format("AsyncWaitHandle.Set() called and returned {0}",b)); } 
+1
source share

I do not think that WaitHandle set in the event descriptor matches the created auto and expects. The BeginReceiveFrom method returns an IAsyncResult object that contains the handle that you are signaling.

Also, if data is waiting, the BeginReceiveFrom method can be processed synchronously (see http://msdn.microsoft.com/en-us/library/system.iasyncresult.completedsynchronously.aspx ). You should probably check this property first before you wait.

0
source share

All Articles