Any advice on how to implement this elegantly?

  • Thread starter Thread starter Fir5tSight
  • Start date Start date
F

Fir5tSight

Hi all,

I have a working C#.NET program. It looks like the follows:

FormMain.cs:

private void buttonCompare_Click(object sender, EventArgs e)
{

foreach (FileName f in allPaceFiles)
{

CompareOnePairOfFiles(f, iCompareType, tol, sw, txtOutput,
FilesProcessed, FilesPassed, FilesFailed);
}

}

private void CompareOnePairOfFiles(FileName f, int iCompareType,
Tolerance tol,
StreamWriter sw, string
txtOutput,
int FilesProcessed, int
FilesPassed, int FilesFailed)
{
// blah blah blah
}

Now I want to improve it to use multiple (two) threads to increase the
speed. Therefore, it looks like the follows:

FormMain.cs:

private void buttonCompare_Click(object sender, EventArgs e)
{

foreach (FileName f in allPaceFiles)
{

Checker checker1 = new Checker(f, iCompareType, tol,
sw, txtOutput, FilesProcessed, FilesPassed, FilesFailed);
checker1.Start();
Checker checker2 = new Checker(f, iCompareType, tol,
sw, txtOutput, FilesProcessed, FilesPassed, FilesFailed);
checker2.Start();

}

}

private void CompareOnePairOfFiles(FileName f, int iCompareType,
Tolerance tol,
StreamWriter sw, string
txtOutput,
int FilesProcessed, int
FilesPassed, int FilesFailed)
{
// blah blah blah
}

Checker.cs:

public class Checker
{

Checker(FileName f, int iCompareType, Tolerance tol,
StreamWriter sw, string txtOutput,
int FilesProcessed, int FilesPassed, int
FilesFailed)
{
// assign these values to members of Checker
}


protected BackgroundWorker worker;

public void Start()
{
if (worker == null)
{
worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(DoWork);
}

if (worker == null)
{
return;
}
}

protected virtual void DoWork(object sender, DoWorkEventArgs e)
{

try
{
// Compare one pair of reports
CompareOnePairOfFiles(f, iCompareType, tol, sw,
txtOutput, FilesProcessed, FilesPassed, FilesFailed);
}
catch (Exception exc)
{
// message
}
}



I get two compiling errors:

1) The name 'CompareOnePairOfFiles' not exist in the current context.
2) 'new Checker(...)' is inaccessible due to its protection level

It seems that there is a flaw in my design. Will anyone advise me on
how to make it work?

Thanks!
-Emily
 
Hi Emily,

The reason for the error "The name 'CompareOnePairOfFiles' not exist
in the current context." could be that, you have the method definition
for "CompareOnePairofFiles" in FormMain.cs and trying to access this
method from Checker.cs.

Take the function definition of "CompareOnePairOfFiles" to Checker.cs
itself and try to compile. It should happen.

The reason for the second error "new Checker(...)' is inaccessible due
to its protection level" could be that, there is no access
modifier(should be public) given for the constructor in the
"Checker.cs" class. Give an access modifier and check it out.

Regards,
Samata.
 
Hi Samata,

Thanks for the suggestions! Let me do as you suggest and see what
happens...

-Emily
 
Hi Samata,

Thanks for the suggestions! Let me do as you suggest and see what
happens...

-Emily
 
I think U make a mistake with thread. use two thread could not risk the
speed of Ur code
 
Back
Top