1

I would like to show a ProgressBar when I call a method, so I have try to do this code:

 if (parametro.chiave == "VIDEO") { //BISOGNA CARICARE IL VIDEO String urlVideo=caricaVideoOnline(parametro.valore); .... } String caricaVideoOnline(string nomeFileVideo) { try { libreriaYouTube = new UploadVideo(ConfigurationManager.AppSettings["usernameYoutube"], ConfigurationManager.AppSettings["passwordYouTube"], ConfigurationManager.AppSettings["developmentKeyYouTube"], ConfigurationManager.AppSettings["applicationNameYouTube"]); libreriaYouTube.listaVideoToUpload.Add(nomeFileVideo); // String video = libreriaYouTube.caricaVideo(); Thread t = new Thread(delegate() { for (int i = 0; i < 100000; i++) { if (i == 0) { ProgressBar progress = new ProgressBar(); progress.Show(); } Console.WriteLine(i); } }); t.SetApartmentState(ApartmentState.STA); t.Start(); String video= libreriaYouTube.caricaVideo(); video = libreriaYouTube.caricaVideo(); return video; } catch (Exception e) { log.Error(e); return null; } } 

This code found but I show the ProgressBar but it is locked, I don't see the bar run.

This is a code of ProgressBar.xaml

<Window x:Class="Gestore.ProgressBar" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Caricamento video" Height="100" Width="300" > <Grid Margin="20"> <ProgressBar Minimum="0" Maximum="100" Name="pbStatus" IsIndeterminate="True" Tag="Attendere....."/> <Viewbox> <TextBlock Text='Attendere ...' FontSize="2" x:Name="textVox"/> </Viewbox> </Grid> </Window> 
2

1 Answer 1

1

I see a couple of issues with your code:

  1. You should access pbStatus, not create a new progress bar in code
  2. You never update the value of your progress bar ("pbStatus.Value=...")
  3. You can't update the progress bar from thread t. You need to invoke the application's UI thread to do this
  4. The loop runs immediately through. Insert some sleep statements to control its timing behaviour.

The following sample code should work.

Xaml:

<StackPanel Orientation="Vertical"> <ProgressBar Name="pbStatus" Minimum="0" Maximum="100" Height="20"></ProgressBar> <Button Click="ButtonBase_OnClick">Press me</Button> </StackPanel> 

Code behind:

 private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { Thread t = new Thread(delegate() { for (int i = 0; i < 100; i++) { this.Dispatcher.Invoke(()=> { pbStatus.Value = i; }); Thread.Sleep(500); } }); t.Start(); } 
Sign up to request clarification or add additional context in comments.

2 Comments

I can't declare in myPb because the application run but it is not visibile, so I declare this ProgressBar p = new ProgressBar()... I try to use this code for (int i = 0; i < 100000; i++) { if (i == 0) { ProgressBar progress = new ProgressBar(); progress.Show(); } Thread.Sleep(500); } but not found
What do you mean with "I can't declare in myPb" ? You can either create a progressbar in XAML or in code. If you do both, you end up with two progress bars, one being visible, the other one invisible. You have to decide for one of the two ways. If you want to create the progress bar in code with "ProgressBar p = new ...", you need to add it to your user interface by adding it to any visible UI container control, like "myGrid.Children.Add(p);". "p.Show()" doesn't work here. My advice: Create pbStatus in XAML as you already did, and use that one. Don't create a new one in code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.