Solved

Responsive WPF UI

Posted on 2014-01-22
3
277 Views
Last Modified: 2014-02-18
I ahve a WPF application not implementing any mVVM.
I have  button click event, where I would like to show a progress Image, and then start a background thread to get some data.
In the background thread when the data has ben retrieved, I want to hide the Progress image.

  private void Button_Click(object sender, RoutedEventArgs e)
        {          
           
            //imgProgress.Visibility = System.Windows.Visibility.Visible;
            Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(
                delegate()
                {
                    imgProgress.Visibility = System.Windows.Visibility.Visible;
                              }));
         
            StartBackgroundThread();
         
        }

 public void StartBackgroundThread()
        {
            BackgroundWorker bgWorker = new BackgroundWorker();
            bgWorker.WorkerSupportsCancellation = true;
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
            bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);          
            bgWorker.RunWorkerAsync();
        }

        void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
       
            BackgroundWorker bgWork = (BackgroundWorker)sender;
            if ((bgWork.CancellationPending == true)) { e.Cancel = true; }
            else
            {
                GetData(0;
            }
        }

 void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
imgProgress.Visibility = System.Windows.Visibility.Hidden;
}

THE UI freezes when the data retrival takes a long time, so does the Progress image.
0
Comment
Question by:countrymeister
  • 2
3 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 39802027
Try making bgWorker a private class variable instead of local.  This will prevent it from being GC, which might be causing the behavior you described.
0
 
LVL 1

Accepted Solution

by:
countrymeister earned 0 total points
ID: 39806864
Tried that but it did not help
0
 
LVL 1

Author Closing Comment

by:countrymeister
ID: 39866902
Please close this question, no expert helped
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now