Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

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.
0
countrymeister
Asked:
countrymeister
  • 2
1 Solution
 
wdosanjosCommented:
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
 
countrymeisterAuthor Commented:
Tried that but it did not help
0
 
countrymeisterAuthor Commented:
Please close this question, no expert helped
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now