Search code examples
c#timerasync-awaitfilestream

Can you have a timer on an async filestream that only counts down when nothing is beeing received?


I'm using a basic file stream to read out a outputReportBuffer through the hid.dll.

As it is setup, i receive messages in real time and i display the data in my program. Currently i am struggling to find a solution around a timer that should start the moment i stop receiving messages from the file stream.

Is there any trigger in the file stream it self that i can use to check whether or not there's still messages coming through?

try
{
    readTrigger = true;
    while (readTrigger)
    {
        await fileStreamDeviceData.ReadAsync(outputReportBuffer, 0, outputReportBuffer.Length);
    }
}
catch (Exception ex)
{
    throw ex;
}

This is how I'm using the File stream and it has been working for me without any issues, until now where i need some sort of trigger/timer to see if if there's still messages coming through.

Any advice on how it could be done better or a pointer in the right direction is greatly appreciated.


Solution

  • ReadAsync accepts a cancellation token. You can pass in a token that will cancel itself after a set period of time. If the task ends up being cancelled then you'll know that the read timed out and can respond accordingly.