Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to use backgrounworker and display progressbar in WPF

$
0
0

Hi Guys,

I would be grateful if somebody help me out to fix this issue. I have a datepickers and a button inside a groupbox in WPF.

I have a progressbar but it's not working when i click on the button.

What i want here, when I click on the button display the progressbar and dsiplay the progress then hide it once the function is done.

How can i apply the background worker to achieve this??

Please explain to me, thank you. Here is my function:

 <ProgressBar Height="25" HorizontalAlignment="Left" Margin="59,75,0,0" Name="progressBar1" VerticalAlignment="Top" Width="688"  Foreground="LawnGreen"/>

 private void b_Calander_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                b_Fifty.IsEnabled = false;
                b_Hundred.IsEnabled = false;
                b_AllImages.IsEnabled = false;
                g_Calander.IsEnabled = false;


                DateTime from = Convert.ToDateTime(datePicker1.SelectedDate.ToString());
                DateTime to = Convert.ToDateTime(datePicker2.SelectedDate.ToString());

                if (datePicker1.SelectedDate > datePicker1.SelectedDate)
                {
                    var verifiydate = d_Language.SelectedItem.ToString() == "French" ? "Veuillez vérifier les dates." : "Please verfiy the dates.";
                    MessageBox.Show(verifiydate, "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation);

                    CheckDevice();
                    return;
                }


           
                var date1 = datePicker1.SelectedDate;
                var date2 = datePicker2.SelectedDate;
                if (date1 == date2)
                {
                    var verifiydate = d_Language.SelectedItem.ToString() == "French" ? "Veuillez vérifier les dates." : "Please verfiy the dates.";
                    MessageBox.Show(verifiydate, "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation);

                    CheckDevice();
                    
                    return;
                }
               
                int FileNb = 0;
                foreach (photo p in photos)
                {
                    if ((p.stamp >= from) && (p.stamp <= to))
                    {
                        FileNb++;
                    }

                    
                    //this.Refresh();
                    //Application.DoEvents();
          
                }
                
                progressBar1.Value = 0;
                progressBar1.Maximum = FileNb;
                progressBar1.Visibility = Visibility.Visible;
                lblInfo.Content = d_Language.SelectedItem.ToString() == "French" ? "Chargement des images...." : "Loading Images...."; //"Chargement des images...";
                lblInfo.Visibility = Visibility.Visible;

                int i = 0;
                foreach (photo p in photos)
                {
                    if ((p.stamp >= from) && (p.stamp <= to))
                    //if (photos.Any(per => (p.stamp >= from) && (p.stamp <= to)))
                    {

                        TransferImageToPC(p);
                        progressBar1.Value = i;
                        i++;

                        var verifyphase1 = "Chargement des images...";// " (" + i + "/" + (FileNb + 1) + ")";
                        var verifyphase12 = "Processing images... ";// "(" + i + "/" + (FileNb + 1) + ")";
                        lblInfo.Content = d_Language.SelectedItem.ToString() == "French" ? verifyphase1 : verifyphase12;
                        //lblInfo.Text = "Chargement des images... (" + i + "/" + (FileNb + 1) + ")";
                    }
                    //this.Refresh();
                    //Application.DoEvents();
                    Application.Current.DoEvents();
                    System.Threading.Thread.Sleep(100);
                }
                progressBar1.Visibility = Visibility.Hidden;
                lblInfo.Visibility = Visibility.Hidden;

                TransfertAndHide();
               
                //this.UseWaitCursor = false;

            }
            catch (Exception)
            {
                lblInfo.Content = d_Language.SelectedItem.ToString() == "French" ? "Le processus été perturbé." : "The Process was Terminated"; //"Le processus été perturbé.";
                progressBar1.Visibility = Visibility.Hidden;
            }
        }



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>