开发者

BackgroundWorker slow

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.ProgressPercenta开发者_如何学Goge;

            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


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)


Umm... you are calling System.Threading.Thread.Sleep() in your code, in a loop, which appears to be calling the query multiple times.

Your update code also seems to be needlessly looping from 0 to the current percentage... not sure why?


Get rid of the whole progress indicator block as it exists. You are iterating over every single record, pausing your thread, re-calling the query and assigning it to e.Result and generally incurring overhead 5,000 times after your data has already loaded. This is useless.

If the data loads in a matter of seconds, show a marquee progressbar on the UI thread and everyone will be happy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜