private static void LoadImages(string InputDirectory, BlockingCollection <BitmapWithFilePathAndSeq> outputQueue, CancellationTokenSource cts)
        {
            int SeqIdNext           = 1;
            CancellationToken token = cts.Token;

            try
            {
                foreach (string filePath in Directory.GetFiles(InputDirectory))
                {
                    if (token.IsCancellationRequested)
                    {
                        break;
                    }

                    if (Path.GetExtension(filePath) == ".bmp")
                    {
                        Bitmap bm = ImageProcessor.LoadFileAsImage(filePath);

                        var outputObj = new BitmapWithFilePathAndSeq()
                        {
                            FilePath = filePath,
                            Image    = bm,
                            SeqId    = SeqIdNext++
                        };
                        outputQueue.Add(outputObj, token);
                    }
                }
            }
            catch (Exception ex)
            {
                if (!(ex is OperationCanceledException))
                {
                    cts.Cancel();
                    throw;
                }
            }
            finally
            {
                outputQueue.CompleteAdding();
            }
        }
        private static void Multiplexer(BlockingCollection <BitmapWithFilePathAndSeq> outputQueue, CancellationTokenSource cts, params BlockingCollection <BitmapWithFilePathAndSeq>[] inputQueues)
        {
            CancellationToken token = cts.Token;
            int nextIndex           = 1;
            var foundItems          = new List <BitmapWithFilePathAndSeq>();

            try
            {
                while (!inputQueues.All(q => q.IsCompleted))
                {
                    if (token.IsCancellationRequested)
                    {
                        break;
                    }

                    BlockingCollection <BitmapWithFilePathAndSeq> .TryTakeFromAny(inputQueues, out var item, MULTIPLEXOR_TIMEOUT, token);

                    if (item != null)
                    {
                        if (item.SeqId == nextIndex)
                        {
                            outputQueue.Add(item, token);

                            BitmapWithFilePathAndSeq nextFound = null;
                            do
                            {
                                if (token.IsCancellationRequested)
                                {
                                    break;
                                }

                                nextIndex++;
                                nextFound = foundItems.Find(i => i.SeqId == nextIndex);

                                if (nextFound != null)
                                {
                                    //foundItems.Remove(nextFound);
                                    outputQueue.Add(nextFound, token);
                                }
                            } while (nextFound != null);
                        }
                        else
                        {
                            foundItems.Add(item);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (!(ex is OperationCanceledException))
                {
                    cts.Cancel();
                    throw;
                }
            }
            finally
            {
                outputQueue.CompleteAdding();
            }
        }