I have a form with a label and a progressbar. I would like to use these to update the user with the progress made on moving some images. Do I use a thread to start the image processing?
Printable View
I have a form with a label and a progressbar. I would like to use these to update the user with the progress made on moving some images. Do I use a thread to start the image processing?
You would use a secondary thread to perform the processing but then you must update the UI on the UI thread. The easiest option would be to use a BackgroundWorker. Follow the CodeBank link in my signature and check out my thread on Using The BackgroundWorker for information and examples.
Thanks. I was looking at BackgroundWorker earlier. It is used in conjunction with a eventhandler, correct?
It is used in exactly the way it is demonstrated in my examples that I provided a link for in my previous post.
OK I am using the following code in a form to move files. The codebank submission helped get that working at least. Thanks to jmcilhinney. However I think that I want a separate class to house the file moving process. Going from what I have so far how can I keep updating the label from this new class? Seems like it should not be hard to do but I am not connecting the dots for whatever reason.
Thanks.
Code:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace VisualScrapUtil
{
public partial class frmUtil : Form
{
public frmUtil()
{
InitializeComponent();
bgWorker.WorkerReportsProgress = true;
bgWorker.WorkerSupportsCancellation = true;
}
private void btnPath_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "Select Images Folder";
if (fbd.ShowDialog() == DialogResult.OK)
{
txtFolder.Text = fbd.SelectedPath;
txtDestination.Text = txtFolder.Text;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (txtFolder.TextLength != 0 || txtDestination.TextLength != 0)
{
if (bgWorker.IsBusy != true)
{
label1.Text = DateTime.Now.ToString();
bgWorker.RunWorkerAsync();
}
}
else
{
label2.Text = "PICK FILES";
}
}
private void btnDest_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "Select Images Destination";
if (fbd.ShowDialog() == DialogResult.OK)
{
txtDestination.Text = fbd.SelectedPath;
}
}
private void btnCancelMove_Click(object sender, EventArgs e)
{
bgWorker.CancelAsync();
}
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
//BackgroundWorker worker = new BackgroundWorker();
//if (worker.CancellationPending == true)
if (bgWorker.CancellationPending == true)
{
e.Cancel = true;
}
else
{
ProcessImages(txtFolder.Text, txtDestination.Text);
}
}
private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label2.Text = (e.ProgressPercentage.ToString() + "%");
}
private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled == true)
{
label2.Text = "CANCELLED";
}
else if (e.Error !=null)
{
label2.Text = "Error: " + e.Error.Message;
}
else
{
label4.Text = DateTime.Now.ToString();
label2.Text= "FINISHED";
}
}
public int ProcessImages(String SourceFolder, String DestinationFile)
{
//leave the fingerprint records alone for now
//only process the .jpg files
try
{
string[] fileImages = Directory.GetFiles(SourceFolder);
Double dblNumFiles = fileImages.Length;
Int32 intPercentDone = 0;
Double dblRecCnt = 0;
foreach (string fileImage in fileImages)
{
dblRecCnt = ++dblRecCnt;
intPercentDone = (int)(((float)dblRecCnt / (float)dblNumFiles) * 100);
bgWorker.ReportProgress(intPercentDone);
if (fileImage.Contains(".jpg"))
{
string[] paths = fileImage.Split('\\');
string newImage = paths[paths.Length - 1];
if (newImage.Substring(0, 2) != "FP")
{
string newFolder = newImage.Substring(2, 8);
string destFilename = DestinationFile + "\\" + newFolder;
if (!Directory.Exists(destFilename))
{
Directory.CreateDirectory(destFilename);
}
destFilename = destFilename + "\\" + newImage;
File.Move(fileImage, destFilename);
}
}
}
}
catch (Exception ex)
{
//do something??
string str = ex.ToString();
}
return 0;
}
}
}
I don't see that you've wired up the bgWorker_ProgressChanged method to the BackgroundWorker (or the _RunWorkerCompleted method, either, whilst we're on the subject).
I dropped a BackgroundWorker control on the form and named it bgWorker. Is this what you mean? I am getting updates in my label2.text control on the form? What have I done incorrectly?
Thanks,
Sorry, I had my head in VB mode so was expecting a "Handles bgWorker.ProgressChanged" on the end of the method signature. If you're getting updates in the label then you have the events wired up, but I thought that your problem was that you weren't getting updates.
Could you clarify please? (What do you expect to see? What do you actually see?)
I want to moveinto a separate class and still be able to update the label on the form. Right now I see the label2.text getting updated with the percentage calculated.Code:public int ProcessImages(String SourceFolder, String DestinationFile)
So move the method. Call it from the same place in the DoWork method via an instance of that class.
Will I need to pass the backgroundworker? Or use a delegate?