Home Async sockets and AutoResetEvent
Reply: 0

Async sockets and AutoResetEvent

user1740 Published in July 16, 2018, 6:12 am

I have an application where the main app creates a loop and waits for a method return some data. i.e

var obj = new MySuperClass();
MyData data; 
while (obj.ReadData(out data)) {
  // do something with data

The data itself comes from an Async socket connection, thus I'm using an AutoResetEvent to signal when data is ready so the main app can consume it.

class MySuperClass {
  private MyData _dataFromSocket;
  private AutoResetEvent _ev = new AutoResetEvent(false);
  public bool ReadData(out MyData data) {
    data = _dataFromSocket;
    // here more logic to return false, but that's not the case or the issue
    return true;  


  /// async socket callback and data parse
  private void MyAsyncCallback(byte rawData) {
    while (canParse(rawData, out _dataFromSocket)) { // somehow parse the raw bytes 
      //if we could parse, signal that we have a packet
      _ev.Set(); // set the signal

When the _ev is set, the Read returns, the main app gets the data and everybody is happy. But in some cases, when AsyncCallback is called with a large buffer where I can deserialize multiple packets, i.e. 5, the main app will receive only 2 or 3, is like the event Skips reseting and Read method doesn't return immediately.

Any idea how to fix this? Is there a faster Event implementation that would signal faster.

I cannot change the architecture of the app, these were the requirements, it must work in the main thread, how data comes it doesn't care, and in this case, I must use async sockets.

Thx for any ideas.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.324988 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO