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.'
Thanks, it worked.
– Tiago Silva