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

  • Home
  • SEARCH
  • 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 8709525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:22:57+00:00 2026-06-13T04:22:57+00:00

I’m creating ViewModel objects in a loop inside BackgroundWorker’s DoWork I’m reporting progress in

  • 0
  • I’m creating ViewModel objects in a loop inside BackgroundWorker’s DoWork
  • I’m reporting progress in each iteration, passing the new object as an argument to be retrieved by ProgressChanged handler (which is friends with the UI thread)
  • In that handler, the object is added to an ObservableCollection with ListBox bound to it.

MY ViewModel class contains two string properties (Filename and ThumbnailPath), and its DataTemplate contains a Label and an Image, bound to these properties.

void bw_DoWork (object sender, DoWorkEventArgs e) {
   List<string> files = e.Argument as List<string>;
   FileInfo fi; int percent;
   for (int i = 0; i < files.Count; i++) {
      FileViewModel newItem = new FileViewModel(files[i]);
      fi = new FileInfo(files[i]);
      percent = i / files.Count * 100;
      bwImportBrowserItems.ReportProgress(percent, newItem);
   }
}

void bw_ProgressChanged (object sender, ProgressChangedEventArgs e) {
    this.observableCollection.Add(e.UserState as FileViewModel);
}

Typical behavior for a typical number of items (30-50): UI freezes for about 2-3 seconds; around a half of the items is displayed; UI freezes again for a shorter time and the rest is added.

Now I understand it’s not the best idea to call UI updates from a loop – I figured the calls come so frequently the UI doesn’t have time to respond to them, that’s why we see the UI being updated “in groups”, leaving it unresponsive in the meantime.

I tried adding Thread.Sleep(500) as the last line of the loop. This helped me illustrate that everything works as it should, because with this slowdown, the items were being added nicely one after another without any nonresponsiveness.

So I tried different sleep values and settled for Thread.Sleep(25). This is not ideal but quite acceptable and comes pretty close to what it should look like.

I’d like to ask if the Thread.Sleep is a common workaround in situations like these, and also what is the general solution people go with in this situation: to update an UI collection from a background loop without ANY unresponsiveness at all.
I’ve also come up with some ideas and I’d appreciate your comments.

Ideas I can think of:

  1. Don’t ReportProgress so often – limit it to 10 times, or to every 10
    new items.
  2. Don’t do it in a loop. Create a list of items that need to be
    created. In the DoWork body, dequeue ONE item from the list,
    instantiate and return ViewModel instance. On RonWorkerCompleted,
    update the UI, check if our list is empty, and if not,
    RunWorkerAsync again.
  • 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-13T04:22:58+00:00Added an answer on June 13, 2026 at 4:22 am

    if the Thread.Sleep is a common workaround in situations like these

    Consider it a last resort. You are increasing the total processing time (not CPU load).

    1) Don’t ReportProgress so often – limit it to 10 times, or to every 10 new items.

    That is the basic idea. Collect new Items and dispatch a List through ReportProgess. Tune the size of the list.

    2) Don’t do it in a loop.

    Possible, but your 1-item per Bgw looks a lot slower. It might even show the same symptoms.

    3) Decouple through a ConcurrentQueue. You can let DoWork fill a Queue and a Dispatcher.Timer could empty it. Also try to process batches with that timer. You can tune the priority of the timer and the batch size.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I have a view passing on information from a database: def serve_article(request, id): served_article
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.