void bgWorkerSearch_DoWork (object sender, DoWorkEventArgs e) {
log.write("We have entered the background worker that does the heavy-lifting in Searches...");
DateTime _start = DateTime.Now;
clsSearcherArgument arg = (clsSearcherArgument)e.Argument;
#region ifDebuggerAttachedGiveSomeInfo
if (Debugger.IsAttached) {
string msg = "Search type: " + ((SearchType)arg.SearchType).ToString() + Environment.NewLine;
msg += "Search ";
msg += (arg.SearchType == (int)SearchType.SearchWithinFiles) ? " phrase: " : " tag(s): ";
if (arg.searchPhraseOrTags.Length > 0) {
msg += arg.searchPhraseOrTags + Environment.NewLine;
} else {
msg += "[none]" + Environment.NewLine;
}
msg += "Author filter: ";
if (arg.searchedForAuthor.Length > 0) {
msg += arg.searchedForAuthor + Environment.NewLine;
} else {
msg += "[none]" + Environment.NewLine;
}
msg += "Time constraint: " + ((TimeConstraint)arg.timeConstraint).ToString();
MessageBox.Show(msg);
}
#endregion
#region setUpNeededVariables
DirectoryInfo di
= new DirectoryInfo
(pathToMyPrayers
); FileInfo[] fi = di.GetFiles();
List
<clsPrayerInfo
> information
= new List
<clsPrayerInfo
>();
int totalPrayerFiles = fi.GetLength(0);
string searchedForAuthor = arg.searchedForAuthor.Trim().ToLower();
string searchedForPhrase = arg.searchPhraseOrTags.Trim().ToLower();
int examined_so_far = 0;
int hits = 0;
int time_constraint = arg.timeConstraint;
double totalSeconds = 0;
bool blnPassesTimeConstraint;
bool blnPassesAuthor;
bool blnPassesSearchPhrase;
decimal fraction = 0;
int the_percent = 0;
for (int i = 0; i < totalPrayerFiles; i++) {
if (fi[i].FullName.ToLower().Contains("thumbs.db")) { totalPrayerFiles--; }
}
#endregion
if (arg.SearchType == (int)SearchType.SearchWithinFiles) {
log.write("About to enter foreach loop for searching within files...");
foreach (FileInfo f in fi) {
blnPassesTimeConstraint = false;
blnPassesAuthor = false;
blnPassesSearchPhrase = false;
if (f.FullName.ToLower().Contains("thumbs.db")) { continue; }
#region codeForExaminingWithinPrayerFileContents
blnPassesAuthor = true; //because SearchWithinFiles doesn't care about the author
FileStream fs
= new FileStream
(f
.FullName, FileMode
.Open, FileAccess
.Read, FileShare
.None); clsPrayerInfo oInfo
= new clsPrayerInfo
();
try {
BinaryFormatter bFormatter
= new BinaryFormatter
(); clsPrayer prayer = (clsPrayer)bFormatter.Deserialize(fs);
string contents = prayer.content.Trim().ToLower();
if (contents.Contains(searchedForPhrase)) {
blnPassesSearchPhrase = true;
oInfo
= new clsPrayerInfo
(prayer, f
.FullName); int selStart = contents.IndexOf(searchedForPhrase);
oInfo.StartOfSelection = selStart;
#region determineIfWePassTheTimeConstraint
if (time_constraint == (int)TimeConstraint.NoConstraint) {
blnPassesTimeConstraint = true;
} else {
DateTime rightNow = DateTime.Now;
TimeSpan ts = rightNow.Subtract(oInfo.lastModified);
totalSeconds = ts.TotalSeconds;
switch (time_constraint) {
case (int)TimeConstraint.PastWeek:
if (totalSeconds <= 604800) { blnPassesTimeConstraint = true; }
break;
case (int)TimeConstraint.PastMonth:
if (totalSeconds <= 2592000) { blnPassesTimeConstraint = true; }
break;
case (int)TimeConstraint.PastYear:
if (totalSeconds <= 31536000) { blnPassesTimeConstraint = true; }
break;
}
}
#endregion
} else {
blnPassesSearchPhrase = false;
}
examined_so_far++;
log.write("We have examined this many files: " + examined_so_far.ToString());
if (blnPassesTimeConstraint && blnPassesSearchPhrase && blnPassesAuthor) {
information.Add(oInfo);
hits++;
}
} catch (Exception exceptionOpeningPrayerFile) {
string msgWithinFilesException = "Error in SearchWithinFiles portion of code in method bgWorkerSearch_DoWork(): " + exceptionOpeningPrayerFile.Message;
log.write(msgWithinFilesException);
MessageBox.Show(msgWithinFilesException, "Error Opening Prayer File During Search");
}
finally {
if (fs != null) { fs.Close(); }
}
#endregion
fraction = (decimal)examined_so_far / (decimal)totalPrayerFiles;
the_percent = Convert.ToInt32(fraction * 100);
bgWorkerSearch.ReportProgress(the_percent);
log.write("Records counted: " + examined_so_far.ToString() + " of " + totalPrayerFiles.ToString() + " (hits: " + hits.ToString() + ") -- " + the_percent.ToString() + " % complete...");
}
} else {
log.write("About to enter foreach loop for searching via tracking files...");
foreach (FileInfo f in fi) {
blnPassesTimeConstraint = false;
blnPassesAuthor = false;
blnPassesSearchPhrase = false;
if (f.FullName.ToLower().Contains("thumbs.db")) { continue; }
#region codeForSearchingAgainstAuthorAndOrTagsUsingTrackingFiles
clsPrayerInfo oInfo
= new clsPrayerInfo
(); string pathToTracker = Path.Combine(pathToTrackingDirectory, Path.GetFileNameWithoutExtension(f.FullName));
if (!File.Exists(pathToTracker)) {
log.write("Tracking file doesn't exist: " + pathToTracker);
continue;
} else {
log.write("The following tracking file DOES exist: " + pathToTracker);
}
FileStream fs
= new FileStream
(pathToTracker, FileMode
.Open, FileAccess
.Read, FileShare
.None); try {
BinaryFormatter bFormatter
= new BinaryFormatter
(); oInfo = (clsPrayerInfo)bFormatter.Deserialize(fs);
oInfo.StartOfSelection = -1;
DateTime rightNow = DateTime.Now;
TimeSpan ts = rightNow.Subtract(oInfo.lastModified);
totalSeconds = ts.TotalSeconds;
log.write("We were able to open the tracker...");
#region determineIfTheTimeConstraintPasses
if (time_constraint == (int)TimeConstraint.NoConstraint) {
blnPassesTimeConstraint = true;
} else {
switch (time_constraint) {
case (int)TimeConstraint.PastWeek:
if (totalSeconds <= 604800) { blnPassesTimeConstraint = true; }
break;
case (int)TimeConstraint.PastMonth:
if (totalSeconds <= 2592000) { blnPassesTimeConstraint = true; }
break;
case (int)TimeConstraint.PastYear:
if (totalSeconds <= 31536000) { blnPassesTimeConstraint = true; }
break;
}
}
#endregion
if (blnPassesTimeConstraint) {
log.write("TimeConstraint passed in Search using Mode Normal or ExactMatch...");
#region determineIfAuthorPasses
if (searchedForAuthor.Length == 0) {
blnPassesAuthor = true;
} else {
if (searchedForAuthor == oInfo.author.Trim().ToLower()) { blnPassesAuthor = true; }
}
#endregion
if (blnPassesAuthor) {
#region determineIfSearchPhrasePasses
if (searchedForPhrase.Length == 0) {
blnPassesSearchPhrase = (arg.SearchType == (int)SearchType.Normal) ? true : false;
} else {
if (arg.SearchType == (int)SearchType.ExactMatch) {
blnPassesSearchPhrase = (searchedForPhrase == oInfo.tags.Trim().ToLower()) ? true : false;
} else {
blnPassesSearchPhrase = (oInfo.tags.Trim().ToLower().Contains(searchedForPhrase)) ? true : false;
}
}
#endregion
}//end if (blnPassesAuthor)
}//end if (blnPassesTimeContraint)
if (blnPassesSearchPhrase && blnPassesAuthor && blnPassesTimeConstraint) {
information.Add(oInfo);
hits++;
}
examined_so_far++;
log.write("We have examined this many files: " + examined_so_far.ToString());
} catch (Exception exception) {
log.write("Exception caught while trying to load clsPrayerInfo data from this tracking file in bgWorkerSearch_DoWork(): " + Path.GetFileName(pathToTracker));
log.write(exception.Message);
}
finally {
if (fs != null) { fs.Close(); }
}
#endregion
fraction = (decimal)examined_so_far / (decimal)totalPrayerFiles;
the_percent = Convert.ToInt32(fraction * 100);
bgWorkerSearch.ReportProgress(the_percent);
log.write("Records counted: " + examined_so_far.ToString() + " of " + totalPrayerFiles.ToString() + " (hits: " + hits.ToString() + ") -- " + the_percent.ToString() + " % complete...");
}
}
DateTime _finished = DateTime.Now;
TimeSpan tsOverall = _finished.Subtract(_start);
string duration = clsGetDurationDesc.getDurationDesc(tsOverall.TotalMilliseconds);
clsSearcherResults oResults
= new clsSearcherResults
(information, duration, examined_so_far, hits
); e.Result = oResults;
log.write("Now exiting bgWorkerSearch_DoWork()...");
}