@ -9,17 +9,23 @@ using System.Threading.Tasks;
using System.Windows.Forms ;
using System.IO ;
using System.Threading ;
using System.Timers ;
namespace fileFinder
{
public partial class MainForm : Form
{
bool hintHided = false ;
TaskController mainController = new TaskController ( ) ;
TaskController mainController ;
System . Timers . Timer updateInfoTimer = new System . Timers . Timer ( 1 0 0 0 ) ;
public MainForm ( )
{
InitializeComponent ( ) ;
mainController = new TaskController ( ) ;
this . updateInfoTimer . Elapsed + = refreshInfoByTimer ;
this . updateInfoTimer . AutoReset = true ;
this . updateInfoTimer . Enabled = true ;
try
{
ImageList iconList = new ImageList ( ) ;
@ -75,71 +81,85 @@ namespace fileFinder
foreach ( String item in foundFiles )
{
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 , "Ошибка поиска внутри файла!" ) ;
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 . currentFileUrl = item ;
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 , "" ) ) ;
}
}
controller . stopTask ( ) ;
return itemsNode ;
}
private List < string > getFileContents ( string fileUrl )
private string [ ] getFileContents ( string fileUrl )
{
List < string > lines = new List < string > ( ) ;
FileStream fStream = new FileStream ( fileUrl , FileMode . Open , FileAccess . Read ) ;
using ( StreamReader sReader = new StreamReader ( fStream , Encoding . UTF8 ) )
try
{
string line ;
while ( ( line = sReader . ReadLine ( ) ) ! = null )
lines . Add ( line ) ;
return File . ReadAllLines ( fileUrl , Encoding . UTF8 ) ;
}
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 )
{
if ( line . IndexOf ( query ) ! = - 1 )
int count = 0 ;
while ( fileLines . Length > count )
if ( fileLines [ count ] . IndexOf ( query ) ! = - 1 )
{
return true ;
else break ;
}
}
count + + ;
return false ;
}
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 )
{
Progress < ProgressReportModel > progress = new Progress < ProgressReportModel > ( ) ;
progress . ProgressChanged + = reportProgress ;
if ( mainController . isStopped )
{
handleSearchBtn . Text = "Pause Task" ;
mainController . beginTask ( ) ;
Progress < ProgressReportModel > progress = new Progress < ProgressReportModel > ( ) ;
progress . ProgressChanged + = reportProgress ;
TreeNode tN = await Task . Run ( ( ) = > buildResultTree ( mainController , progress ) ) ;
resultViewer . Nodes . Clear ( ) ;
resultViewer . Nodes . Add ( tN ) ;
updateResultViewer ( tN ) ;
handleSearchBtn . Text = "Start Task" ;
} else if ( mainController . isPaused )
{
handleSearchBtn . Text = "Pause Task" ;
mainController . resumeTask ( ) ;
} else
{
handleSearchBtn . Text = "Resume Task" ;
mainController . pauseTask ( ) ;
}
}
@ -171,5 +191,35 @@ namespace fileFinder
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 { }
}
}
}