Solved

Responsive WPF UI

Posted on 2014-01-22
3
274 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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

11 Experts available now in Live!

Get 1:1 Help Now