I have this c# code to show a progressbar:
{
public partial class FormPesquisaFotos : Form
{
public FormPesquisaFotos()
{
InitializeComponent();
}
private void FormPesquisaFotos_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//Mostra a barra de progresso da pesquisa
while (progressBar1.Value < 100)
progressBar1.Value += 1;
{
//Criar um objeto (instância, cópia) da classe FormResultadosFotos
FormResultadosFotos NovoForm = new FormResultadosFotos();
NovoForm.Show();
}
}
}
}
It only loads at the end of the search (after button click). How can I have progressbar running at the beginning of the search?
Here is the code to show the results on a new form. Progressbar stops at 95% and after a few seconds it shows the results.
{
public partial class FormResultadosFotos : Form
{
public FormResultadosFotos()
{
InitializeComponent();
}
private void FormFotos_Load(object sender, EventArgs e)
{
// se pretendermos pesquisar em várias pastas
List<string> diretorios = new List<string>()
{@"\\Server\folder01\folder02"};
// se pretendermos pesquisar as várias extensões
List<string> extensoes = new List<string>()
{".jpg",".bmp",".png",".tiff",".gif"};
DataTable table = new DataTable();
table.Columns.Add("Nome e formato do ficheiro (duplo clique para visualizar a imagem)");
table.Columns.Add("Caminho ( pode ser copiado Ctrl+C )");
foreach (string diretorio in diretorios)
{
var ficheiros = Directory.EnumerateFiles(diretorio, "*", SearchOption.AllDirectories).
Where(r => extensoes.Contains(Path.GetExtension(r.ToLower())));
foreach (var ficheiro in ficheiros)
{
DataRow row = table.NewRow();
row[0] = Path.GetFileName(ficheiro);
row[1] = ficheiro;
table.Rows.Add(row);
}
}
dataGridView1.DataSource = table;
dataGridView1.Columns[1].Visible = true;
}
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
FormPictureBox myForm = new FormPictureBox();
string imageName = dataGridView1.CurrentRow.Cells[1].Value.ToString();
Image img;
img = Image.FromFile(imageName);
myForm.pictureBox1.Image = img;
myForm.ShowDialog();
}
}
}
Thank you.
You must have it on a new thread and not on the main thread.
Here is a little example:
The
Dispacher.Invoke
is because its on WPF, for WinForm just change it tothis.Invoke
This example, is when the button is clicked the BackgroundWorker it will start, there is a for to go from 1 to 100 and it will sleep 100 miliseconds and will update the progressbar.
Hope that this it will help out
Edit
Did include now on the example the event to run when the BackgroundWorker finnish, just in case if there is the need for it
Edit 2:
My advice is when searching for the photos on the background insert them into a DataTable all rdy, since your all ready searching for them and the work can be done here also, then just make a constructor on the FormResultadosFotos that will receive that DataTable.
From what I did understood the main goal to was to search them on the form FormPesquisaFotos (thats why we have the background worker there, to search for them and update the ProgressBar) and show them on the new form AKA FormResultadosFotos
In here you could all also to see what the table bring's by puting a breakpoint on the line
this.dataGridView1.DataSource = table;
, if the table is empty there was nothing introduced into the table (maybe no photos on the directory ? Can't access to it? Not at work and don't have any IDE with me, just basing my anwser from what I have in my head, but you could also get the files on a simular code if needed:Edit 3:
Change on your code the
to
You need the * before the extension (.png as example) to search files for that extension
What you're describing here is a multi threading issue. Your loop runs before the UI has a chance to update itself.
You should check out https://stephenhaunts.com/2014/10/14/using-async-and-await-to-update-the-ui-thread/ for an explanation & an example on how to update the UI while you're in a loop.
Regards