Link to home
Start Free TrialLog in
Avatar of countrymeister
countrymeister

asked on

Responsive WPF UI

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.
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

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.
ASKER CERTIFIED SOLUTION
Avatar of countrymeister
countrymeister

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of countrymeister
countrymeister

ASKER

Please close this question, no expert helped