Why doesn’t my Progressbar increase values?

Asked

Viewed 17 times

0

I have an application in Winforms where I have Tasks and in each task I implement statically a value there progressBar, I would just like to know why it does not show this evolution in winForms Ui.

private async void btnTratar_Click_1(object sender, EventArgs e)
    {
        int i;

        progressBar1.Minimum = 0;
        progressBar1.Maximum = 100;


        await Task.Run(() =>
        {

            pastaTiff = tbCaminho.Text + @"\TIFF";
            pastaJpeg = tbCaminho.Text + @"\JPEG";
            pastaJpegWM = tbCaminho.Text + @"\JPEGWM";
            if (!Directory.Exists(pastaTiff))
            {
                Directory.CreateDirectory(pastaTiff);
            }
            if (!Directory.Exists(pastaJpeg))
            {
                Directory.CreateDirectory(pastaJpeg);
            }

            DirectoryInfo Parent = Directory.GetParent(tbCaminho.Text);

            if (Directory.Exists(Parent.ToString()))
            {
                pastaTiffWM = Parent.ToString() + @"\TIF_MarcadAgua";
                Directory.CreateDirectory(pastaTiffWM);


            }

            if (!Directory.Exists(pastaJpegWM))
            {
                Directory.CreateDirectory(pastaJpegWM);
            }

            if (Directory.Exists(Parent.ToString()))
            {

                pastaPDFTxt = Parent.ToString() + @"\PDF+TXT";
                Directory.CreateDirectory(pastaPDFTxt);

            }


        });

        List<string> files = new List<string>();

        foreach (string file in Directory.GetFiles(tbCaminho.Text, "*.tif"))
        {
            files.Add(file);
        }

        foreach (string file in files)
        {
            FileInfo fInfo = new FileInfo(file);
            if (dirName == "")
            {
                string[] hlpr = fInfo.Name.Split('_');
                dirName = hlpr[0];
                break;
            }
        }


        await Task.Run(() => {
            TifConverter(files, pastaJpeg, pastaTiff);
            i = 20;
            progressBar1.Value = i;
            
        });
        //WaterMarker(filesJpg);

        await Task.Run(() => {
            WaterMarker(filesJpg);
            i = 40;
            progressBar1.Value = i;
        });

        List<string> filesWM = new List<string>();

        foreach (string file in Directory.GetFiles(pastaTiffWM, "*.tif"))
        {
            filesWM.Add(file);
        }

        await Task.Run(() =>
        {
            TifConverterWM(filesWM, pastaJpegWM, pastaTiff);
            i = 60;
            progressBar1.Value = i;
        });

        await Task.Run(() => 
        {
            ConvertToPDF(pastaJpegWM, pastaPDFTxt, dirName);
            i = 80;
            progressBar1.Value = i;
        });
           
        //ConvertToPDF(pastaJpegWM, pastaPDFTxt, dirName);
        DirectoryInfo dirInfo = new DirectoryInfo(tbCaminho.Text);
        tvResultado.AfterSelect += tvResultado_AfterSelect;
        BuildTree(dirInfo, tvResultado.Nodes);
        MessageBox.Show("Conversão concluída com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
        dirName = "";



        DisposeJPGWM(pastaJpegWM);
        DisposeJpg(pastaJpeg);

        i = 100;
        progressBar1.Value = i;
    }

The main problem I find is this mistake.

System.InvalidOperationException: 'A operação entre threads não é válida: controlo 'progressBar1' acedido a partir de um thread diferente do thread onde foi criado.'

1 answer

1


In this case you are trying to modify controls within a thread that is not what it belongs to. To solve you can involve these controls as follows:

Invoke((MethodInvoker)delegate
       {
           labelX.Text = "Teste A";
           labelY.Text = "Teste B";
           labelZ.Text = "Teste C";
           progressBar1.Value = i;
       });

Another detail, if you want to keep your code, just do the following in the last line containing the bar Progress:

await Task.Run(() => 
    {
        progressBar1.Value = i;
    });

I believe you forgot to implement await when you put progressBar1.Value = i; on the last line.

  • Thanks, it worked.

Browser other questions tagged

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