I have a main module
that asynchronously calls a function in another module. That function performs an asynchronous read on a Filestream.
This is the function call in the main module:
// fileStreamDeviceData is a Filestream
// inputReportBuffer is a byte array to hold the data being read.
var isuccess = await MyHid.GetInputReportViaInterruptTransfer
(fileStreamDeviceData, inputReportBuffer);
In the MyHid module, the GetInputReportViaInterruptTransfer function contains this code:
// OnTimeout is another function in the module.
Action onReadTimeoutAction = OnTimeout;
var cts = new CancellationTokenSource();
cts.CancelAfter(5000);
cts.Token.Register(() => onReadTimeoutAction());
Task t = fileStreamDeviceData.ReadAsync(inputReportBuffer, 0, inputReportBuffer.Length, cts.Token);
await t;