Solved

Responsive WPF UI

Posted on 2014-01-22
3
284 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to sort columns in DataGridView 4 36
Coding for the first time 9 62
ModalPopup  question 22 37
Installing .NET 3.5 on Windows Server 2012 1 55
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

713 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