@ -9,17 +9,23 @@ using System.Threading.Tasks;
using System.Windows.Forms ;
using System.Windows.Forms ;
using System.IO ;
using System.IO ;
using System.Threading ;
using System.Threading ;
using System.Timers ;
namespace fileFinder
namespace fileFinder
{
{
public partial class MainForm : Form
public partial class MainForm : Form
{
{
bool hintHided = false ;
bool hintHided = false ;
TaskController mainController = new TaskController ( ) ;
TaskController mainController ;
System . Timers . Timer updateInfoTimer = new System . Timers . Timer ( 1 0 0 0 ) ;
public MainForm ( )
public MainForm ( )
{
{
InitializeComponent ( ) ;
InitializeComponent ( ) ;
mainController = new TaskController ( ) ;
this . updateInfoTimer . Elapsed + = refreshInfoByTimer ;
this . updateInfoTimer . AutoReset = true ;
this . updateInfoTimer . Enabled = true ;
try
try
{
{
ImageList iconList = new ImageList ( ) ;
ImageList iconList = new ImageList ( ) ;
@ -75,71 +81,85 @@ namespace fileFinder
foreach ( String item in foundFiles )
foreach ( String item in foundFiles )
{
{
while ( controller . isPaused )
while ( controller . isPaused )
Thread . Sleep ( 1 0 0 ) ;
List < string > fileLines = new List < string > ( ) ;
try
{
fileLines = getFileContents ( item ) ;
}
catch ( Exception ex )
{
{
MessageBox . Show ( ex . Message , "Ошибка поиска внутри файла!" ) ;
Thread . Sleep ( 1 0 0 ) ;
if ( ! mainController . isFormUpdatedAfterPause )
{
mainController . isFormUpdatedAfterPause = true ;
_ report . progress = counter ;
_ report . currentFileUrl = item ;
_ report . currentTreeNode = ( TreeNode ) itemsNode . Clone ( ) ;
report . Report ( _ report ) ;
}
}
}
string [ ] fileLines = getFileContents ( item ) ;
_ report . progress = counter ;
_ report . progress = counter ;
_ report . currentFileUrl = item ;
_ report . currentFileUrl = item ;
report . Report ( _ report ) ;
report . Report ( _ report ) ;
if ( ( fileLines . Count > 0 ) & & ( isFileContainQuery ( fileLines , innerQueryTextBox . Text ) ) )
if ( ( fileLines . Length > 0 ) & & ( isFileContainQuery ( fileLines , innerQueryTextBox . Text ) ) )
{
counter + + ;
await fillChildNode ( itemsNode , item . Replace ( curDirTextBox . Text , "" ) ) ;
await fillChildNode ( itemsNode , item . Replace ( curDirTextBox . Text , "" ) ) ;
}
}
}
controller . stopTask ( ) ;
controller . stopTask ( ) ;
return itemsNode ;
return itemsNode ;
}
}
private List < string > getFileContents ( string fileUrl )
private string [ ] getFileContents ( string fileUrl )
{
{
List < string > lines = new List < string > ( ) ;
try
FileStream fStream = new FileStream ( fileUrl , FileMode . Open , FileAccess . Read ) ;
using ( StreamReader sReader = new StreamReader ( fStream , Encoding . UTF8 ) )
{
{
string line ;
return File . ReadAllLines ( fileUrl , Encoding . UTF8 ) ;
while ( ( line = sReader . ReadLine ( ) ) ! = null )
lines . Add ( line ) ;
}
}
return lines ;
catch ( Exception ex )
{
MessageBox . Show ( ex . Message , "Ошибка поиска внутри файла!" ) ;
}
return null ;
}
}
private bool isFileContainQuery ( List < string > fileLines , string query )
private bool isFileContainQuery ( string [ ] fileLines , string query )
{
{
foreach ( string line in fileLines )
int count = 0 ;
{
while ( fileLines . Length > count )
if ( line . IndexOf ( query ) ! = - 1 )
if ( fileLines [ count ] . IndexOf ( query ) ! = - 1 )
{
return true ;
return true ;
else break ;
}
}
count + + ;
return false ;
return false ;
}
}
private void reportProgress ( object sender , ProgressReportModel report )
private void reportProgress ( object sender , ProgressReportModel report )
{
{
infoLabel . Text = report . currentFileUrl ;
if ( ! mainController . isPaused )
updateInfoLabel ( report . currentFileUrl , report . progress ) ;
else if ( mainController . isPaused )
updateResultViewer ( report . currentTreeNode ) ;
}
}
private async void handleSearchBtn_Click ( object sender , EventArgs e )
private async void handleSearchBtn_Click ( object sender , EventArgs e )
{
{
Progress < ProgressReportModel > progress = new Progress < ProgressReportModel > ( ) ;
progress . ProgressChanged + = reportProgress ;
if ( mainController . isStopped )
if ( mainController . isStopped )
{
{
handleSearchBtn . Text = "Pause Task" ;
mainController . beginTask ( ) ;
mainController . beginTask ( ) ;
Progress < ProgressReportModel > progress = new Progress < ProgressReportModel > ( ) ;
progress . ProgressChanged + = reportProgress ;
TreeNode tN = await Task . Run ( ( ) = > buildResultTree ( mainController , progress ) ) ;
TreeNode tN = await Task . Run ( ( ) = > buildResultTree ( mainController , progress ) ) ;
resultViewer . Nodes . Clear ( ) ;
updateResultViewer ( tN ) ;
resultViewer . Nodes . Add ( tN ) ;
handleSearchBtn . Text = "Start Task" ;
} else if ( mainController . isPaused )
} else if ( mainController . isPaused )
{
{
handleSearchBtn . Text = "Pause Task" ;
mainController . resumeTask ( ) ;
mainController . resumeTask ( ) ;
} else
} else
{
{
handleSearchBtn . Text = "Resume Task" ;
mainController . pauseTask ( ) ;
mainController . pauseTask ( ) ;
}
}
}
}
@ -171,5 +191,35 @@ namespace fileFinder
break ;
break ;
}
}
}
}
public void updateInfoLabel ( string fileUrl , int counter )
{
TimeSpan tS = mainController . elapsedTime ( ) ;
string time = String . Format ( "{0:00}:{1:00}:{2:00}" , tS . Hours , tS . Minutes , tS . Seconds ) ;
infoLabel . Text = "Time: " + time + " " + "| Processed files: " + counter +
" | Current file: " + fileUrl ;
}
public void updateResultViewer ( TreeNode tN )
{
if ( tN ! = null )
{
resultViewer . Nodes . Clear ( ) ;
resultViewer . Nodes . Add ( tN ) ;
}
}
public void refreshInfoByTimer ( object sender , ElapsedEventArgs e )
{
Action refresh = ( ) = >
{
TimeSpan tS = mainController . elapsedTime ( ) ;
string time = String . Format ( "{0:00}:{1:00}:{2:00}" , tS . Hours , tS . Minutes , tS . Seconds ) ;
infoLabel . Text = "Time: " + time + " |" +
infoLabel . Text . Substring ( infoLabel . Text . IndexOf ( "|" ) + 1 ) ;
} ;
try { this . Invoke ( refresh ) ; } catch { }
}
}
}
}
}