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

108
fileFinder/MainForm.cs

@ -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(1000);
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(100); Thread.Sleep(100);
List<string> fileLines = new List<string>(); if (!mainController.isFormUpdatedAfterPause)
try {
{ mainController.isFormUpdatedAfterPause = true;
fileLines = getFileContents(item); _report.progress = counter;
} _report.currentFileUrl = item;
catch (Exception ex) _report.currentTreeNode = (TreeNode) itemsNode.Clone();
{ report.Report(_report);
MessageBox.Show(ex.Message, "Ошибка поиска внутри файла!"); }
} }
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 { }
}
} }
} }

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.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms;
namespace fileFinder namespace fileFinder
{ {
@ -10,5 +11,6 @@ namespace fileFinder
{ {
public int progress { get; set; } public int progress { get; set; }
public string currentFileUrl { get; set; } public string currentFileUrl { get; set; }
public TreeNode currentTreeNode { get; set; }
} }
} }

14
fileFinder/TaskController.cs

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

Loading…
Cancel
Save