0

I wrote a windows phone 8 app that using StreamSocket class, but found a strange situation. My code is as below

async void Connect() {

  await StreamSocketConnect();

  TryToSendAsync();

  ReceiveAsync();
}

async void ReceiveAsync() { 
                            //WriteLine("111");
   await DataReader.LoadAsync();
                            //WriteLine("222");
   DoReceiverCallBack();
}

async void TryToSendAsync(){

   await DataWriter.StoreAsync();

 }

void DoReceiveCallBack() { 
                             //WriteLine("333");
   ReceiveAsync();
                             //WriteLine("444");
}

In sequence, it should follow 333 => 111 => 444 => 222, but sometime I will get 333 => 111 => 222 => 444 while receiving data especially. Will it be synchronous sometimes??

svick
  • 236,525
  • 50
  • 385
  • 514

1 Answers1

2

Yes, DataReader.LoadAsync may complete synchronously.

Sockets have their own buffer; as data arrives on the socket, the OS will buffer it for you until you can read it. If there is enough data in the buffer for LoadAsync to complete, then it will be synchronous.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810