I'm working on an app for Windows CE 5.0 , by .NET CF 3.5 SP1. i wanna simulate socket receive timeout and wrote 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's 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 app and logged some info, i receive data immediately and 'ar.AsyncWaitHandle.Set()' returns true, but why program flow never ends? what's wrong?
I should pass the WaitHandle i created for example as StateObject parameter to my BeginReceiveFrom method that i can access it later in callback method. i edited my code and it works now. in fact , i think the related samples 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));
}