This seems impossible. You can think of BlockingCollection as a FIFO queue, you can only add or delete data in order.
But there is a way to try it, convert it into a list, insert the data into the list, and then create a new BlockingCollection based on the list. It is not elegant, but it may suit your requirements.
switch (type)
{
case 1:
//ListPackage.Add(currentPackage);
BlockingCollectionPackage.Add(currentPackage);
break;
case 2:
{
List<Package> packages = BlockingCollectionPackage.ToList();
packages.Insert(0, currentPackage);
BlockingCollectionPackage = new BlockingCollection<Package>(new ConcurrentQueue<Package>(packages));
break;
}
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.