The main (only) reason that you can't just databind to the BlockingCollection is that it does not implement INotifyCollectionChanged, so the UI would not get updated when a request is added / removed. That said, there are BlockingCollection wrappers around on the net that implement INotifyCollectionChanged. Or you could instead expose a CollectionView for the BlockingCollection and instead of dispatching a remove from an ObservableCollection, just dispatch a Refresh of the CollectionView.
public ICollectionView UploadRequestsView {get;set;}
public UploadRequester(Dispatcher dispatcher)
{
this.dispatcher = dispatcher;
this.uploadRequestsBlocking = new BlockingCollection<UploadRequest>();
UploadRequestsView = CollectionViewSource.GetDefaultView(uploadRequestsBlocking);
this.consumerTask = Task.Factory.StartNew(this.ConsumeUploadRequests);
}
public void AddUploadRequest(UploadRequest uploadRequest)
{
uploadRequestsBlocking.Add(uploadRequest);
UploadRequestsView.Refresh()
}
private void ConsumeUploadRequests()
{
foreach (var uploadRequest in this.uploadRequestsBlocking.GetConsumingEnumerable())
{
uploadRequest.Status = "Uploading...";
Thread.Sleep(2000);
uploadRequest.Status = "Successfully uploaded";
Thread.Sleep(500);
dispatcher.Invoke(() => UploadRequestsView.Refresh());
}
}