Progress bar in second window

1

I'm creating a system that takes files from a server and copies it to my machine. I work in a software company and as we can not install systems from outside, I decided to create one myself. The system is up and running, but I'm improving it.

I need to insert a progress bar so I can track the status of the copy. I created another Form2.cs window where the toolbar is, I configured the call of the window, however I am in doubt when making the call of the bar for loading.

Follow 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!!");
        }
    
asked by anonymous 06.03.2018 / 07:38

1 answer

3

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

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!!");
      }
    });
}

Do not forget to add a ProgressBar with the name of progressBar1 on your Form2. The same will be the progress bar.

Notice that there is Task.Run in the above code. This is due to the fact that when we need to do heavy tasks, such as copying files from one place to another, this type of task must be executed in a different% of% other than% with%. And that's exactly what Thread does. It executes the code entered in a new UI Thread , a Task.Run in the background.

Note also that in% wc% used,% wc%, I use% wc%. This is due to the fact that in order to update thread , we need to execute the change in thread . And that's just what ProgressBar is doing.

In your% of_%, what you should do is just call progressBar1 :

private void button1_Click(object sender, EventArgs e)
{
    FormProgress progress = new FormProgress();
    progress.Show();
}
    
06.03.2018 / 12:37