J'ai un cas d'utilisation dans lequel je souhaite insérer et supprimer un objet personnalisé (Stocks) d'une collection de blocage (image plus grande représentant une file d'attente de producteur).Suppression d'éléments de BlockingCollection et remplissage de la collection observable
L'énoncé du problème est exactement semblable à ce fil - update an ObservableCollection with a BlockingCollection
Je ne veux pas utiliser des extensions réactives mais veulent une manière traditionnelle C# pour faire cette logique (ce qui est une exigence difficile à comprendre, malheureusement, et complètement les implications). Mon bout de code est ici
MainWindowViewModel.cs
public class MainWindow_VM : ViewModelBase
{
public ObservableCollection<StockModel> stocks { get; set; }
private readonly Dispatcher currentDispatcher;
private BlockingCollection<StockModel> tasks = new BlockingCollection<StockModel>();
#endregion
// All other standard ViewModel logic - Constructor, Command etc
private void handlermethod(object sender, MarketDataEventArgs e)
{
Task.Factory.StartNew(AddUpdateObservableCollection);
// Below thought process (maybe wrong) - How do i add the value to the BlockingCollection through a thread considering I have a ProducerConsumer class standard implementation (which has Enqueue and Dequeue Methods)
using (ProducerConsumerQueue q = new ProducerConsumerQueue())
{
foreach (Stock s in e.updatedstock)
{
StockModel sm = new StockModel();
sm.Symbol = s.Symbol;
sm.Bidprice = s.Bidprice;
q.EnqueueTask(s);
}
}
private void AddUpdateObservableCollection()
{
//Signalling mechanism still missing - when Stock comes into BlockingCollection - then this will start draining.
// Also have to take care of Dispatcher stuff since you can only update ObservableCollection through Dispatcher
foreach (StockModel sm in tasks)
{
if (sm != null)
{
if (stocks.Any(x => x.Symbol == sm.Symbol))
{
var found = stocks.FirstOrDefault(x => x.Symbol == sm.Symbol);
int i = stocks.IndexOf(found);
stocks[i] = sm;
}
else
{
stocks.Add(sm);
}
}
}
}
}
Vous ajoutez et prenez à partir d'un BlockingCollection. – Paparazzi