I have found that the C# serial port implementation can be pretty flaky when it comes to the datarecieved
event, so I decided to implement my own asynchronous read using the underlying stream
. I implemented a continuous read loop that I would expect to get a stack overflow due to recursion, but for some reason it doesn't. Here is the code:
SerialPort port = new SerialPort("COM0");
Stream bar = port.BaseStream;
Action foo = null;
foo = () =>
{
byte[] buf = new byte[256];
AsyncCallback callback = ar =>
{
int bytesRead = bar.EndRead(ar);
//call event sending recieved bytes to main program
foo();
};
bar.BeginRead(buf, 0, 8, callback, null);
};
foo();
I suspect the recursive calls to foo
to keep making new stack frames until the system would crash, but apparently that is not the case.
So my question is, why do the recursive calls to foo
not cause a stack overflow?