Hier is one my example of Producer-Consumer Pattern.
First I want to read all files from HDD and put them in the queue (producer):
private void SearchForAllFilesAndPutThemInQueue(BlockingCollection<string> fileQueue, string path)
{
foreach (string file in Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories))
{
fileQueue.Add(file);
}
}
Then I will read file names from queue and do something with (consumer):
private void ReadFileNamesFromQueue
(
BlockingCollection<string>? fileQueue
, BlockingCollection<(LatLngModel, string)>? gpsInfoQueue
)
{
int i = 0;
if (fileQueue is null) throw new ArgumentNullException(nameof(fileQueue));
if (gpsInfoQueue is null) throw new ArgumentNullException(nameof(gpsInfoQueue));
foreach (string file in fileQueue.GetConsumingEnumerable())
{
try
{
ExtractGpsInfoFromImageCommand extractGpsInfoFromImageCommand = new ExtractGpsInfoFromImageCommand();
extractGpsInfoFromImageCommand.ImageFileNameToReadGpsFrom = file;
extractGpsInfoFromImage.Execute(extractGpsInfoFromImageCommand);
if (extractGpsInfoFromImageCommand.LatLngModel is not null)
{
gpsInfoQueue.Add((extractGpsInfoFromImageCommand.LatLngModel, file));
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message());
}
}
}
Here you can download my example, little bit complicated, where I get list of file in one queue, in second I try to extract GPS info, and from third queue I am saving to CSV file file name and longitude / latitude.