❔ Producer Consumer ISourceBlock<T>, periodically flushing consumed data

Hi,
I've the following scenario: I've a camera continuously acquiring frames and a c# app receiving it and storing it in a
List<MemoryStream>
. Once per minute, the memory stream is copied, passed to a task in charge of rendering a video file from such frames, and then emptying the original stream in order to use it for consequent frames.

Now I'm trying to perform video rendering in real-time, as frames are received by the program. I've seen this guide from Microsoft about using
ISourceBlock<T>
to implement a buffer that can be used in a producer-consumer scenario. This is good for me, since I can have a task receiving data from the camera and producing data in the buffer and a parallel task consuming data from the buffer.
The problem is, once per minute I need the rendering task (the consuming one) to finalize the video file it has been adding frames to. At the same time, I need the producer task to continue producing frames, in the same buffer or in a different one.

In order to accomplish this, the only way that came up to my mind is the following:
1) when the producer start producing data belonging to a new video file that's supposed to belong to a new video, it starts a new task, passing to it bufferA. The consumer will start consume data as it is produced.
2) when the producer has produced all data related to the video file that is being parallely rendered, it calls
ITargetBlock<T>.Complete()
to notify the consumer that frames for this video have finished and it needs to finalize the video rendering.
3) then the procuder will continue producing data, this time putting it in a bufferB.
4) rendering of frames in buffer B is completed
5) buffer A is cleared and producer start filling it again
etc...
Was this page helpful?