bircastri
bircastri

Reputation: 2169

How to show ProgressBar when method is call

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>

Upvotes: 1

Views: 1275

Answers (1)

Frank im Wald
Frank im Wald

Reputation: 918

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();
    }

Upvotes: 1

Related Questions