-
Feb 11th, 2012, 04:50 PM
#1
Thread Starter
Fanatic Member
Do I use a thread to update a label from a class?
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?
-
Feb 12th, 2012, 12:37 AM
#2
Re: Do I use a thread to update a label from a class?
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.
-
Feb 12th, 2012, 12:39 AM
#3
Thread Starter
Fanatic Member
Re: Do I use a thread to update a label from a class?
Thanks. I was looking at BackgroundWorker earlier. It is used in conjunction with a eventhandler, correct?
-
Feb 12th, 2012, 01:24 AM
#4
Re: Do I use a thread to update a label from a class?
It is used in exactly the way it is demonstrated in my examples that I provided a link for in my previous post.
-
Feb 14th, 2012, 02:30 PM
#5
Thread Starter
Fanatic Member
Re: Do I use a thread to update a label from a class?
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;
}
}
}
-
Feb 14th, 2012, 06:13 PM
#6
Re: Do I use a thread to update a label from a class?
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).
-
Feb 14th, 2012, 09:11 PM
#7
Thread Starter
Fanatic Member
Re: Do I use a thread to update a label from a class?
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,
-
Feb 14th, 2012, 09:27 PM
#8
Re: Do I use a thread to update a label from a class?
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?)
-
Feb 14th, 2012, 09:29 PM
#9
Thread Starter
Fanatic Member
Re: Do I use a thread to update a label from a class?
I want to move
Code:
public int ProcessImages(String SourceFolder, String DestinationFile)
into 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.
-
Feb 14th, 2012, 09:39 PM
#10
Re: Do I use a thread to update a label from a class?
So move the method. Call it from the same place in the DoWork method via an instance of that class.
-
Feb 15th, 2012, 03:53 PM
#11
Thread Starter
Fanatic Member
Re: Do I use a thread to update a label from a class?
Will I need to pass the backgroundworker? Or use a delegate?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|