Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8944229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:58:39+00:00 2026-06-15T11:58:39+00:00

Hei, In my application i’m using DataGrid to show some data. To get everything

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:58:40+00:00Added an answer on June 15, 2026 at 11:58 am

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hei. I am using the POST method for sending some information from a JSP
Using the documentation provided by Apple to create an application preferences window that doesn't
I'm trying to create an application that can show a list of options from
Hei! I need to optimize some matrix multiplication code in c, and I'm doing
Hei, I'm trying to fill a textfield declared in a jsp file with some
Hei there, I started using ajaxForm plugin, having the form statement like this: <form
Hei guys, I'm trying to load a long text from a .rtf file and
Hei, I have a main report with Portrait layout, but the main report contains
Hei there, I'm not experienced at all in C++ as I need to start
After compiling my application in Netbeans and running the application in Netbeans it works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.