In some place I call
Task.Run(new Action(Read));
this call starts task in other thread:
private void Read()
{
while (!cancelToken.IsCancellationRequested)
{
int bytesReaded = deviceStream.Read(buffer, 0, bufferSize);
}
}
This thread blocks while Read try to get some data from file (this is not usual file, this is device). then I close application, form closed, but application still runing. I tried Close and Dispose on deviceStream but not helped - its still wait to Read completed. Interrupting thread not recommended by Microsoft.
The main goal is read data non-stop, but data arrives from device by pieces, and pause between pieces can be huge, so Read can be blocked for the long time.
Questions: Is there some method to gracefully interrupt Read? Is there another pattern can be implemented to achieve my goal (with BeginRead/EndRead maybe, but readed here that EndRead also blocks and can't be interrupted.
PS: I see new method in MSDN ReadAsync, but can't imagine how I can use it (there I need restart Read)
It was not FileStream.Read issue, I blocked ReadDispatcher in driver using KeWait... function.