Continued code refactoring to avoid threading issues

This commit is contained in:
rlv-dan 2020-04-16 18:56:41 +02:00
parent 10ce610db0
commit 9087cd2748
3 changed files with 78 additions and 61 deletions

View File

@ -226,6 +226,9 @@
8 or so other bug fixes and minor enhancements
Internal code refactoring in preparation for future features
v2.11 (2020-04-18)
Fixed a threading issue that caused the program to hang on some systems
--- End User License Agreement -----------------------------------------------

View File

@ -13,62 +13,6 @@ namespace Snap2HTML
{
class Utils
{
// Recursive function to get all folders and subfolders of given path path
public static void DirSearch( string sDir, List<string> lstDirs, bool skipHidden, bool skipSystem, Stopwatch stopwatch, BackgroundWorker backgroundWorker)
{
if( backgroundWorker.CancellationPending ) return;
try
{
foreach( string d in System.IO.Directory.GetDirectories( sDir ) )
{
bool includeThisFolder = true;
//if( d.ToUpper().EndsWith( "SYSTEM VOLUME INFORMATION" ) ) includeThisFolder = false;
// exclude folders that have the system or hidden attr set (if required)
if( skipHidden || skipSystem )
{
var attr = new DirectoryInfo( d ).Attributes;
if( skipHidden )
{
if( ( attr & FileAttributes.Hidden ) == FileAttributes.Hidden )
{
includeThisFolder = false;
}
}
if( skipSystem )
{
if( ( attr & FileAttributes.System ) == FileAttributes.System )
{
includeThisFolder = false;
}
}
}
if( includeThisFolder )
{
lstDirs.Add( d );
if(stopwatch.ElapsedMilliseconds >= 50)
{
backgroundWorker.ReportProgress( 0, "Getting folders... " + lstDirs.Count + " (" + d + ")" );
stopwatch.Restart();
}
DirSearch( d, lstDirs, skipHidden, skipSystem, stopwatch, backgroundWorker );
}
}
}
catch( System.Exception ex )
{
Console.WriteLine( "ERROR in DirSearch():" + ex.Message );
}
}
// Hack to sort folders correctly even if they have spaces/periods in them
public static List<string> SortDirList( List<string> lstDirs )
{
@ -132,5 +76,15 @@ namespace Snap2HTML
{
return (int)Math.Truncate( ( value.ToUniversalTime().Subtract( new DateTime( 1970, 1, 1 ) ) ).TotalSeconds );
}
public static long ParseLong(string s)
{
long num;
if( Int64.TryParse( s, out num ) )
{
return num;
}
return 0;
}
}
}

View File

@ -31,6 +31,8 @@ namespace Snap2HTML
return;
}
backgroundWorker.ReportProgress( 0, "Processing content..." );
// Calculate some stats
int totDirs = 0;
int totFiles = 0;
@ -41,7 +43,7 @@ namespace Snap2HTML
foreach( var file in folder.Files )
{
totFiles++;
totSize += Int64.Parse( file.GetProp( "Size" ) );
totSize += Utils.ParseLong( file.GetProp( "Size" ) );
}
}
@ -133,6 +135,9 @@ namespace Snap2HTML
backgroundWorker.ReportProgress( 100, "Ready!" );
}
// --- Helper functions (must be static to avoid thread problems) ---
private static List<SnappedFolder> GetContent( SnapSettings settings, BackgroundWorker bgWorker )
{
var stopwatch = new Stopwatch();
@ -143,7 +148,7 @@ namespace Snap2HTML
// Get all folders
var dirs = new List<string>();
dirs.Insert( 0, settings.rootFolder );
Utils.DirSearch( settings.rootFolder, dirs, settings.skipHiddenItems, settings.skipSystemItems, stopwatch, bgWorker );
DirSearch( settings.rootFolder, dirs, settings.skipHiddenItems, settings.skipSystemItems, stopwatch, bgWorker );
dirs = Utils.SortDirList( dirs );
if( bgWorker.CancellationPending )
@ -263,6 +268,63 @@ namespace Snap2HTML
return result;
}
// Recursive function to get all folders and subfolders of given path path
private static void DirSearch( string sDir, List<string> lstDirs, bool skipHidden, bool skipSystem, Stopwatch stopwatch, BackgroundWorker backgroundWorker )
{
if( backgroundWorker.CancellationPending ) return;
try
{
foreach( string d in System.IO.Directory.GetDirectories( sDir ) )
{
bool includeThisFolder = true;
//if( d.ToUpper().EndsWith( "SYSTEM VOLUME INFORMATION" ) ) includeThisFolder = false;
// exclude folders that have the system or hidden attr set (if required)
if( skipHidden || skipSystem )
{
var attr = new DirectoryInfo( d ).Attributes;
if( skipHidden )
{
if( ( attr & FileAttributes.Hidden ) == FileAttributes.Hidden )
{
includeThisFolder = false;
}
}
if( skipSystem )
{
if( ( attr & FileAttributes.System ) == FileAttributes.System )
{
includeThisFolder = false;
}
}
}
if( includeThisFolder )
{
lstDirs.Add( d );
if( stopwatch.ElapsedMilliseconds >= 50 )
{
backgroundWorker.ReportProgress( 0, "Getting folders... " + lstDirs.Count + " (" + d + ")" );
stopwatch.Restart();
}
DirSearch( d, lstDirs, skipHidden, skipSystem, stopwatch, backgroundWorker );
}
}
}
catch( System.Exception ex )
{
Console.WriteLine( "ERROR in DirSearch():" + ex.Message );
}
}
private static string BuildJavascriptContentArray(List<SnappedFolder> content, int startIndex, BackgroundWorker bgWorker)
{
// Data format:
@ -275,8 +337,6 @@ namespace Snap2HTML
// ID is the item index in dirs array.
// Note: Modified date is in UNIX format
bgWorker.ReportProgress( 0, "Processing content..." );
var result = new StringBuilder();
var lineBreakSymbol = ""; // Could be set to \n to make the html output more readable, at the expense of increased size
@ -332,7 +392,7 @@ namespace Snap2HTML
sbCurrentDirArrays.Append( "\"" ).Append( Utils.MakeCleanJsString( currentFile.Name ) ).Append( "*" ).Append( currentFile.GetProp( "Size" ) ).Append( "*" ).Append( currentFile.GetProp( "Modified" ) ).Append( "\"," + lineBreakSymbol );
try
{
dirSize += Int64.Parse( currentFile.GetProp("Size") );
dirSize += Utils.ParseLong( currentFile.GetProp( "Size" ) );
}
catch( Exception ex)
{