Hei,
In my application i'm using DataGrid to show some data. To get everything working with threading i'm using AsyncObservableCollection
as DataContext of DataGrid. When my application starts it looks for files in some folders and updates AsyncObservableCollection
. Finding files is done on a separate thread:
Task.Factory.StartNew(() => _cardType.InitAllOrdersCollection())
.ContinueWith((t) => ThrowEvent(), TaskContinuationOptions.None);
Where all the loading logic is in InitAllOrdersCollection()
method.
Now here's where things go bad, when i start the application for some reason i get 2 rows with same data in DataGrid even if there is one item in collection and only one file in folders. If i add a delay(Thread.Sleep()
50ms minimum) before loading files then DataGrid show everything correctly (no extra row). Delay has to be added to the Thread what is loading the files (The one created with Task.Factory.StartNew()
).
Have anybody encountered something similar or is there something else i should try? Thanks in advance!
EDIT: Adding some code as requested:
public AsyncObservableCollection<IGridItem> OrdersCollection = new AsyncObservableCollection<IGridItem>();
public void InitAllOrdersCollection()
{
// Thread.Sleep(50); <-- this sleep here fixes the problem!
foreach (var convention in FileNameConventions)
{
var namePatterns = convention.NameConvention.Split(',');
foreach (var pattern in namePatterns)
{
var validFiles = CardTypeExtensions.GetFiles(this.InputFolder, pattern, convention);
if (validFiles.Any())
{
this.FilesToOrders(validFiles, convention);
}
}
}
}
public static List<string> GetFiles(string inputFolder, string pattern, FileNameConvention convention)
{
var files = Directory.GetFiles(inputFolder, pattern);
return files.Where(file => IsCorrect(file, convention)).AsParallel().ToList();
}
// Adds new order to OrdersCollection if its not there already!
private void FilesToOrders(List<string> dirFiles, FileNameConvention convention)
{
foreach (var dirFile in dirFiles.AsParallel())
{
var order = new Order(dirFile, this, convention);
if (!this.OrdersCollection.ContainsOrder(order))
{
this.OrdersCollection.Add(order);
}
}
}
public static bool ContainsOrder(this ObservableCollection<IGridItem> collection, Order order)
{
return collection.Cast<Order>().Any(c=>c.Filepath == order.Filepath);
}
FilesToOrders()
method is the one what adds the new orders to the AsyncObservableCollection
.
Hope this helps.