Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid is adding extra "ghost" row

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.

like image 390
hs2d Avatar asked Nov 29 '12 11:11

hs2d


2 Answers

Add CanUserAddRows="False" to your XAML file

<DataGrid CanUserAddRows="False"../>
like image 81
Lana Avatar answered Nov 04 '22 21:11

Lana


Maybe I'm missing something obvious, but the AsyncObservableCollection implementation in the link you posted doesn't look thread-safe to me.

I can see it includes code to fire the CollectionChanged / PropertyChanged events on the creator (consumer) thread, but I don't see any synchronization to make access to the items in the collection thread-safe.

UPDATE

As far as I can see you can have the following happening concurrently, without any synchronization:

  • the worker (producer) thread is inserting item(s)

  • the UI (consumer) thread is enumerating items

One possibility might be to modify AsyncObservableCollection.InsertItem to call SynchronizationContext.Send to insert the item on the consumer thread, though this will of course have an effect on performance (producer waits for consumer thread to complete insertion before continuing).

An alternative approach would be to use a standard ObservableCollection that is only ever accessed on the consumer thread, and use SynchronizationContext.Post to post items to insert from the producer thread. Something like:

foreach (var dirFile in dirFiles.AsParallel())
{
    var order = new Order(dirFile, this, convention);

    _synchronizationContext.Post(AddItem, order);

}

...

void AddItem(object item)
{
    // this is executed on the consumer thread
    // All access to OrderCollection on this thread so no need for synchnonization
    Order order = (Order) item;
    if (!OrdersCollection.ContainsOrder(order))
    {
        OrdersCollection.Add(order);
    }
}
like image 3
Joe Avatar answered Nov 04 '22 22:11

Joe