Browse Source

buildResultTree теперь использует string[] вместо List<string> для поиска внутри файла. Во время паузы resultView обновляется, а обновлением теперь занимается метод updateResultViewer. Нижний инфобар теперь работает полноценно (updateInfoLabel, refreshInfoByTimer).

release
Никита 6 years ago
parent
commit
27a952505f
  1. 9
      fileFinder/MainForm.Designer.cs
  2. 108
      fileFinder/MainForm.cs
  3. 1608
      fileFinder/MainForm.resx
  4. 2
      fileFinder/ProgressReportModel.cs
  5. 14
      fileFinder/TaskController.cs

9
fileFinder/MainForm.Designer.cs

@ -28,6 +28,7 @@
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.resultViewer = new System.Windows.Forms.TreeView();
this.dirSelectBtn = new System.Windows.Forms.Button();
this.curDirTextBox = new System.Windows.Forms.TextBox();
@ -74,6 +75,7 @@
// splitContainer
//
this.splitContainer.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
this.splitContainer.Location = new System.Drawing.Point(0, 0);
this.splitContainer.Name = "splitContainer";
this.splitContainer.Orientation = System.Windows.Forms.Orientation.Horizontal;
@ -134,13 +136,11 @@
// infoLabel
//
this.infoLabel.AutoSize = true;
this.infoLabel.Dock = System.Windows.Forms.DockStyle.Fill;
this.infoLabel.Location = new System.Drawing.Point(0, 0);
this.infoLabel.Name = "infoLabel";
this.infoLabel.Size = new System.Drawing.Size(423, 13);
this.infoLabel.Size = new System.Drawing.Size(263, 13);
this.infoLabel.TabIndex = 0;
this.infoLabel.Text = "Current file is: C:\\nextcloud\\scripts\\... | Number of handled files: 123 | It\'s b" +
"een 30 secs...\r\n";
this.infoLabel.Text = "Time: 00:00:00 | Processed files: 0 | Current item: none";
//
// MainForm
//
@ -148,6 +148,7 @@
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.splitContainer);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "MainForm";
this.Text = "fileFinder Utility Created by Defend";
this.splitContainer.Panel1.ResumeLayout(false);

108
fileFinder/MainForm.cs

@ -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(1000);
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(100);
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 { }
}
}
}

1608
fileFinder/MainForm.resx

File diff suppressed because it is too large

2
fileFinder/ProgressReportModel.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace fileFinder
{
@ -10,5 +11,6 @@ namespace fileFinder
{
public int progress { get; set; }
public string currentFileUrl { get; set; }
public TreeNode currentTreeNode { get; set; }
}
}

14
fileFinder/TaskController.cs

@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace fileFinder
{
@ -10,17 +12,21 @@ namespace fileFinder
{
public bool isPaused { get; private set; }
public bool isStopped { get; private set; }
public bool isFormUpdatedAfterPause { get; set; }
private Stopwatch stopwatch = new Stopwatch();
public TaskController ()
{
isPaused = false;
isStopped = true;
isFormUpdatedAfterPause = true;
}
public bool beginTask ()
{
isPaused = false;
isStopped = false;
stopwatch.Restart();
return true;
}
@ -29,6 +35,8 @@ namespace fileFinder
if (!isPaused && !isStopped)
{
isPaused = true;
isFormUpdatedAfterPause = false;
stopwatch.Stop();
return true;
} else
return false;
@ -39,6 +47,7 @@ namespace fileFinder
if (isPaused && !isStopped)
{
isPaused = false;
stopwatch.Start();
return true;
} else
return false;
@ -49,10 +58,15 @@ namespace fileFinder
if (!isStopped)
{
isStopped = true;
stopwatch.Stop();
return true;
} else
return false;
}
public TimeSpan elapsedTime ()
{
return stopwatch.Elapsed;
}
}
}

Loading…
Cancel
Save