Progress bar in second window

Asked

Viewed 314 times

1

I’m creating a system that takes files from a server and copies them to my machine. I work in a software company and as we can not install systems from outside, I decided to create the own. The system is even working, but I’m improving it.

I need to insert a progress bar so that it is possible to track the status of the copy. I created another Form2.Cs window where the bar is, I configured the call from the window, but I’m in doubt when it’s time to make the call from the load bar.

Follows the code:

private void button1_Click(object sender, EventArgs e)
    {
        //Chamar outra janela 

        atualizainstaladores2.Form2 barra = new atualizainstaladores2.Form2();
        barra.ShowDialog();

        if (!Directory.Exists(@"E:\InstaladoresSHOP\Sistemas"))
        {
            //criar o diretório caso não exista
            // copiar de PDV todos os arquivos com .exe no final 
            Directory.CreateDirectory(@"E:\InstaladoresShop\Sistemas\Shop\PDV");
            string[] PDV = Directory.GetFiles(@"C:\Users\thale\Downloads\Sistemas\Sistemas\PDV", "*.exe");
            foreach (string item in PDV)
            {

                File.Copy(item, @"E:\InstaladoresShop\Sistemas\Shop\PDV\" + Path.GetFileName(item));
            }



            MessageBox.Show("Diretorio PDV atualizado com Sucesso!!");
        }
  • 2

    1- Code execution will stop on ShowDialog up to the form barra be closed, then you can not open it as a modal dialog. 2- During the copy, the screen will be locked. The ideal is that this task is done in another thread, while the progress is displayed on the screen. 3-Take a look at the Backgroundworker, and consider displaying the progress bar in it Form where the copy is happening.

  • 1

    I implemented what @Rovannlinhalis commented above in my reply.

1 answer

3


Since you want a second Form with a progress bar, I suggest you transfer the code from the copy of the files to that second Form. The moment you call it, the copying process will start and the screen will be updated with the progress of the copy.

Put the following code in your Form2:

public Form2()
{
    InitializeComponent();

    StartCopy();
}

private void StartCopy()
{
    string[] PDV = Directory.GetFiles(@"C:\Users\thale\Downloads\Sistemas\Sistemas\PDV", "*.exe");

    // Coloca a quantidade de arquivos como valor máximo do ProgressBar
    progressBar1.Maximum = PDV.Length;
    progressBar1.Value = 0;

    Task.Run(() =>
    {
       if (!Directory.Exists(@"E:\InstaladoresSHOP\Sistemas"))
       {
        Directory.CreateDirectory(@"E:\InstaladoresShop\Sistemas\Shop\PDV");

        // Inicia a cópia
        foreach (string item in PDV)
        {
            File.Copy(item, @"E:\InstaladoresShop\Sistemas\Shop\PDV\" + Path.GetFileName(item));
            progressBar1.Invoke((Action)delegate { progressBar1.Value += 1; });
        }

        MessageBox.Show("Diretorio PDV atualizado com Sucesso!!");
      }
    });
}

Don’t forget to add one ProgressBar by the name of progressBar1 in your Form2. The same will be the progress bar.

Realize that there is a Task.Run in the code above. This is due to the fact that when we need to do heavy tasks, such as copying files from one side to the other, this type of task must be performed in another Thread other than UI Thread. And that’s exactly what the Task.Run makes. Executes the informed code in a new thread, one thread in the background.

Also note that in the ProgressBar used, progressBar1, I use a Invoke. This is due to the fact that to update the ProgressBar, we need to perform the modification on UI Thread. And that’s exactly what the Invoke is making.

In his Form1, what you should do is just call the Form2:

private void button1_Click(object sender, EventArgs e)
{
    FormProgress progress = new FormProgress();
    progress.Show();
}
  • 1

    I would only change it so as not to charm GetFiles twice, call once and store the values =]

  • 1

    @Rovannlinhalis Well observed. Also because I was not doing the test whether the folder existed or not, which could generate error. I put in only one place. Thanks for the tip.

  • 1

    Perfect, worked perfectly. thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.