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 3598406
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:14:17+00:00 2026-05-18T20:14:17+00:00

i am developing a project (WPF) and i have a Datagrid the load more

  • 0

i am developing a project (WPF) and i have a Datagrid the load more than 5000 records from the database so i used a BackgroundWorker to advice the user the data is loading but it is so slow , i need to wait almost 2 minutes to load the data from the database,instead if i don’t use the BackgroundWorker i need to wait just 3 second to load the data in the Datagrid.

Here i write down the code snippet that i use for the BackgroundWorker :

   private void RunWorker()
    {
        worker = new BackgroundWorker();
        worker.WorkerReportsProgress = true;
        worker.WorkerSupportsCancellation = true;
        worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
        worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
        worker.RunWorkerAsync();
    }



    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker senderWorker = sender as BackgroundWorker;
        dc = new DataClasses1DataContext();
        var query = from c in dc.Contact_DDBB_Xavis
                    select
                        new
                        {
                            c.ContactID,
                            c.Continent,
                            c.Country,
                            c.City,
                            c.PostalCode,
                            c.CompanyName,
                            c.UserCreated,
                            c.DateCreated,
                            c.UserModified,
                            c.DateModified
                        };

        if (query.Count() > 0)
        {

            for (int i = 0; i < query.Count(); i++)
            {

                int progressInPercent = (int)(((decimal)(i + 1) / (decimal)query.Count()) * 100);
                worker.ReportProgress(progressInPercent, i);

                System.Threading.Thread.Sleep(10);
                e.Result = query.ToList();

            }
        }
        if (senderWorker.CancellationPending)
        {
            e.Cancel = true;
        }
    }

    private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            this.dataGrid.DataContext = e.Result;

            backGround.Visibility = Visibility.Collapsed;
            duracel.Visibility = Visibility.Collapsed;
            txtBackWORK.Visibility = Visibility.Collapsed;

        }
    }


    private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        for (double i = 0.0; i < e.ProgressPercentage; i++)
        {
            duracel.pthFiller.Width = 0;
            double max = 312;
            max = (double)e.ProgressPercentage;

            duracel.pthFiller.Width = e.ProgressPercentage * 3.12;

            duracel.txtStatus.Text = e.ProgressPercentage + " %";
            txtBackWORK.Text = String.Format("Loading " + e.ProgressPercentage + " %");

        }
    }

now i don’t know if there is something wrong in my code so i ask you some advice to how load faster the data from database without wait so long time.

Thanks for your attention.

Have a good time.

Cheers

  • 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-05-18T20:14:18+00:00Added an answer on May 18, 2026 at 8:14 pm

    Each time you call query.Count(), you’re running another SQL query.
    You should call Count() once, and store it in a local variable.

    Also, you should only call `ReportProgress if the progress actually changed. (There’s no point in calling it 1,000 times)

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

Sidebar

Related Questions

No related questions found

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.