0

Is it possible to run a progressbar in windows phone 8 on a separate thread, or using the Dispatcher class?

thanx

Bohrend
  • 1,467
  • 3
  • 17
  • 26

2 Answers2

3

put ur progress bar in App.xaml

<ProgressBar IsIndeterminate="True" HorizontalAlignment="Stretch" VerticalAlignment="Top" Loaded="ProgressBar_Loaded" Visibility="Collapsed" />

& then put this c# code into ur App.xaml.cs

public partial class App : Application
    {
        // progress bar
        public ProgressBar m_progressBar;
        // some code......


        private void ProgressBar_Loaded(object sender, RoutedEventArgs e)
        {
            m_progressBar = (ProgressBar)sender;
        }
    }

define a separate method in a class

public static Class RestMethods
{
   public static void ShowProgress(bool show)
        {
            if (show)
                ((App)Application.Current).m_progressBar.Visibility = Visibility.Visible;
            else
                ((App)Application.Current).m_progressBar.Visibility = Visibility.Collapsed;
        }
}

now u have separate method to show or hide the progress bar

call it in a dispature, so it wont freez ur UI--->

Dispatcher.BeginInvoke(() => { RestMethods.ShowProgress(true); });    // to show
Dispatcher.BeginInvoke(() => { RestMethods.ShowProgress(false); });    // to hide
Ashok Damani
  • 3,896
  • 4
  • 30
  • 48
  • @Ashok, what is the reason to put ProgressBar to App.xaml? To have an access to ProgressBar from everywhere in app? Anyway Dispatcher.BeginInvoke(() => { RestMethods.ShowProgress(true); }); will be called in UI thread, Dispatcher will marshal a call to UI thread. Moreover ProgressBar in WP8 doesn't freeze UI thread. – Pavel Saniuk Jun 26 '13 at 19:49
0

UI controls in WindowsPhone can be updated just from UI thread and it is not possible to update ProgressBar from separate (not UI) thread. Dispatcher just will marshal a call from working thread to UI thread.

Pavel Saniuk
  • 788
  • 5
  • 11