Backgroundworker

R

Rotsey

Hi,

Anyone able to look at this code with backgroundworker.

I am calling a FindDuplicates class and need to pass in
the DriveData class that is passed into the form.

So it is necessary to pass in to RunWorkerAsync the
DriveData object or can I just access it from the form
level variable.???

Also I am passing into the FindDuplicates a ref to the background worker
so it can report progress using the ReportProgress method,
is this the right way???

And thirdly the is passing the result of FindDuplicates to
RunWorkerCompleted correct??

Malcolm




public partial class frmDuplicates : Form

{

private DriveData mDriveData;

private BackgroundWorker mWorker;

public frmDuplicates(DriveData data)

{

mDriveData = data;

InitializeComponent();

mWorker = new BackgroundWorker();

mWorker.DoWork += new DoWorkEventHandler(mWorker_DoWork);

mWorker.WorkerReportsProgress = true;

mWorker.ProgressChanged += new
ProgressChangedEventHandler(mWorker_ProgressChanged);

mWorker.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(mWorker_RunWorkerCompleted);

mWorker.RunWorkerAsync(mDriveData);

}

void mWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs
e)

{

FileDuplicates dup = e.Result as FileDuplicates;

FileExplorer exp = new FileExplorer(dup.Files, mDriveData);

splitContainer1.Panel1.Controls.Add(exp);

}

void mWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)

{

this.Text = e.ProgressPercentage.ToString("0%");

}

void mWorker_DoWork(object sender, DoWorkEventArgs e)

{

DriveData data = e.Argument as DriveData;

FileDuplicates dup = new FileDuplicates(data);

if (dup.FindDuplicates(mWorker))

{

e.Result = dup;

}

}

private void frmDuplicates_Load(object sender, EventArgs e)

{

}

}
 
C

Chris Shepherd

Rotsey said:
I am calling a FindDuplicates class and need to pass in
the DriveData class that is passed into the form.

So it is necessary to pass in to RunWorkerAsync the
DriveData object or can I just access it from the form
level variable.???

It should be passed in -- it's why the Argument-taking overload is there.
Also I am passing into the FindDuplicates a ref to the background worker
so it can report progress using the ReportProgress method,
is this the right way???

And thirdly the is passing the result of FindDuplicates to
RunWorkerCompleted correct??

Yes and no. The sender object in DoWork will always been the
BackgroundWorker itself, so I think for proper encapsulation you'd need
to cast that as the BackgroundWorker rather than passing in the
reference the form has (and really, ignoring encapsulation if you
already have access somewhere, just use that, no point in duplicating it).

IE:

if (dup.FindDuplicates(sender as BackgroundWorker))
e.Result = dup;

As for the result, maybe this is just semantics, but you don't actually
use the result of FindDuplicates except as a boolean. Yes, that is the
correct way to assign the result.

Chris.
 
R

Rotsey

Understand all you say except about the result

the dup object has a generic list that is the result of the find duplicates
so that is what I want to have access to when it has completed.

so my code is correct for that??
 
C

Chris Shepherd

Rotsey said:
Understand all you say except about the result

the dup object has a generic list that is the result of the find duplicates
so that is what I want to have access to when it has completed.

so my code is correct for that??

Judging by that, no. If FindDuplicates is defined something like:

public List<DuplicateItem> FindDuplicates (BackgroundWorker bw)

Then no, that will not work. What you're really looking to do is redo this:

if (dup.FindDuplicates(sender as BackgroundWorker))
e.Result = dup;

To this:

e.Result = dup.FindDuplicates(sender as BackgroundWorker);



Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top