Browse Source

Управление текущей задачей переосмыслено через enum TaskState. Удалены неиспользуемые using *.

release
Никита 6 years ago
parent
commit
d5e2011976
  1. 98
      fileFinder/MainForm.cs
  2. 7
      fileFinder/ProgressReportModel.cs
  3. 85
      fileFinder/TaskController.cs

98
fileFinder/MainForm.cs

@ -1,14 +1,7 @@
using System; using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing; using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Timers; using System.Timers;
namespace fileFinder namespace fileFinder
@ -19,6 +12,7 @@ namespace fileFinder
TaskController mainController = new TaskController(); TaskController mainController = new TaskController();
SearchQueryModel searchQueryModel; SearchQueryModel searchQueryModel;
System.Timers.Timer updateInfoTimer = new System.Timers.Timer(1000); System.Timers.Timer updateInfoTimer = new System.Timers.Timer(1000);
bool isFieldsChanged = false;
public MainForm() public MainForm()
{ {
@ -38,7 +32,7 @@ namespace fileFinder
queryTextBox.Text = sets.fileNameQuery; queryTextBox.Text = sets.fileNameQuery;
searchQueryModel = mainController.updateSearchQueryModel(sets.fileUrl, searchQueryModel = mainController.updateSearchQueryModel(sets.fileUrl,
sets.fileInnerQuery, sets.fileNameQuery); sets.fileInnerQuery, sets.fileNameQuery);
mainController.isTaskChanged = false; isFieldsChanged = false;
} }
private void fillImagesToResultViewer() private void fillImagesToResultViewer()
@ -81,10 +75,15 @@ namespace fileFinder
private void reportProgress(object sender, ProgressReportModel report) private void reportProgress(object sender, ProgressReportModel report)
{ {
if (!mainController.isPaused) switch (mainController.state)
updateInfoLabel(report.currentFileUrl, report.progress); {
else if (mainController.isPaused) case TaskState.Work:
updateResultViewer(report.currentTreeNode); updateInfoLabel(report.currentFileUrl, report.progress);
break;
case TaskState.Pause:
updateResultViewer(report.currentTreeNode);
break;
}
} }
private async void handleSearchBtn_Click(object sender, EventArgs e) private async void handleSearchBtn_Click(object sender, EventArgs e)
@ -92,45 +91,54 @@ namespace fileFinder
Progress<ProgressReportModel> progress = new Progress<ProgressReportModel>(); Progress<ProgressReportModel> progress = new Progress<ProgressReportModel>();
progress.ProgressChanged += reportProgress; progress.ProgressChanged += reportProgress;
if (mainController.isStopped || mainController.isTaskChanged) if (isFieldsChanged) // если в форме изменился запрос
mainController.stopTask(); // то принудительно завершаем задание
isFieldsChanged = false;
switch (mainController.state)
{ {
mainController.stopTask(); case TaskState.Created:
mainController = new TaskController(); searchQueryModel = mainController.updateSearchQueryModel(curDirTextBox.Text,
searchQueryModel = mainController.updateSearchQueryModel(curDirTextBox.Text, queryTextBox.Text, innerQueryTextBox.Text);
queryTextBox.Text, innerQueryTextBox.Text); handleSearchBtn.Text = "Pause Task";
handleSearchBtn.Text = "Pause Task"; mainController.beginTask();
mainController.beginTask();
changeVisibilityResultViewer();
TreeNode tN = await Task.Run(() => mainController.buildResultTree(searchQueryModel, progress));
if (tN != null)
{
updateResultViewer(tN);
changeVisibilityResultViewer(); changeVisibilityResultViewer();
handleSearchBtn.Text = "Start Task"; TreeNode tN = await Task.Run(() => mainController.buildResultTree(searchQueryModel, progress));
} if (tN != null)
} else if (mainController.isPaused) {
{ updateResultViewer(tN);
handleSearchBtn.Text = "Pause Task"; handleSearchBtn.Text = "Start Task";
mainController.resumeTask(); }
changeVisibilityResultViewer(); break;
} else case TaskState.Work:
{ handleSearchBtn.Text = "Resume Task";
handleSearchBtn.Text = "Resume Task"; mainController.pauseTask();
mainController.pauseTask(); break;
changeVisibilityResultViewer(); case TaskState.Pause:
handleSearchBtn.Text = "Pause Task";
mainController.resumeTask();
break;
case TaskState.Finished:
mainController = new TaskController();
goto case TaskState.Created;
} }
changeVisibilityResultViewer();
} }
private void changeVisibilityResultViewer() private void changeVisibilityResultViewer()
{ {
if (mainController.isStopped || mainController.isPaused) switch (mainController.state)
{
waitOrPauseLabel.Visible = false;
resultViewer.Enabled = true;
} else
{ {
waitOrPauseLabel.Visible = true; case TaskState.Work: // если задание в работе, то блокируем ResultViewer
resultViewer.Enabled = false; waitOrPauseLabel.Visible = true;
resultViewer.Enabled = false;
break;
case TaskState.Pause: // если задание на паузе, то показываем промеж. результат
waitOrPauseLabel.Visible = false;
resultViewer.Enabled = true;
break;
case TaskState.Finished: // если задание завершено, то показываем результат
goto case TaskState.Pause;
} }
} }
@ -189,9 +197,9 @@ namespace fileFinder
private void changeTaskBtnStatus() private void changeTaskBtnStatus()
{ {
if (!mainController.isTaskChanged) if (!isFieldsChanged)
{ {
mainController.isTaskChanged = true; isFieldsChanged = true;
handleSearchBtn.Text = "Start New Task"; handleSearchBtn.Text = "Start New Task";
} }
} }

7
fileFinder/ProgressReportModel.cs

@ -1,9 +1,4 @@
using System; using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace fileFinder namespace fileFinder
{ {

85
fileFinder/TaskController.cs

@ -6,63 +6,46 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms; using System.Windows.Forms;
namespace fileFinder namespace fileFinder
{ {
public enum TaskState { Created, Work, Pause, Finished }
class TaskController class TaskController
{ {
public bool isPaused { get; private set; }
public bool isStopped { get; private set; }
public bool isFormUpdatedAfterPause { get; set; }
public bool isTaskChanged { get; set; }
private Stopwatch stopwatch = new Stopwatch(); private Stopwatch stopwatch = new Stopwatch();
public TaskState state { get; private set; }
public TaskController () public TaskController()
{ {
isPaused = false; state = TaskState.Created;
isStopped = true;
isFormUpdatedAfterPause = true;
isTaskChanged = false;
} }
public void beginTask () public void beginTask()
{ {
isPaused = false; state = TaskState.Work;
isStopped = false;
stopwatch.Restart(); stopwatch.Restart();
} }
public void pauseTask () public void pauseTask()
{ {
if (!isPaused && !isStopped) state = TaskState.Pause;
{ stopwatch.Stop();
isPaused = true;
isFormUpdatedAfterPause = false;
stopwatch.Stop();
}
} }
public void resumeTask () public void resumeTask()
{ {
if (isPaused && !isStopped) state = TaskState.Work;
{ stopwatch.Start();
isPaused = false;
stopwatch.Start();
}
} }
public void stopTask () public void stopTask()
{ {
if (!isStopped) state = TaskState.Finished;
{ stopwatch.Stop();
isStopped = true;
stopwatch.Stop();
}
} }
public TimeSpan elapsedTime () public TimeSpan elapsedTime()
{ {
return stopwatch.Elapsed; return stopwatch.Elapsed;
} }
@ -77,30 +60,30 @@ namespace fileFinder
int counter = 0; int counter = 0;
foreach (string item in foundFiles) foreach (string item in foundFiles)
{ {
if (controller.isStopped) switch (state)
return null;
while (controller.isPaused)
{ {
Thread.Sleep(100); case TaskState.Finished:
if (!controller.isFormUpdatedAfterPause) return null;
{ case TaskState.Pause:
controller.isFormUpdatedAfterPause = true;
_report.progress = counter; _report.progress = counter;
_report.currentFileUrl = item; _report.currentFileUrl = item;
_report.currentTreeNode = (TreeNode)itemsNode.Clone(); _report.currentTreeNode = (TreeNode)itemsNode.Clone();
report.Report(_report); report.Report(_report);
} while (state == TaskState.Pause)
} Thread.Sleep(100);
goto case TaskState.Work;
string[] fileLines = getFileContents(item); case TaskState.Work:
_report.progress = counter; string[] fileLines = getFileContents(item);
_report.currentFileUrl = item; _report.progress = counter;
report.Report(_report); _report.currentFileUrl = item;
report.Report(_report);
if ((fileLines.Length > 0) && (isFileContainQuery(fileLines, query.fileInnerQuery))) if ((fileLines.Length > 0) && (isFileContainQuery(fileLines, query.fileInnerQuery)))
{ {
counter++; counter++;
await fillChildNode(itemsNode, item.Replace(query.fileUrl, "")); await fillChildNode(itemsNode, item.Replace(query.fileUrl, ""));
}
break;
} }
} }
controller.stopTask(); controller.stopTask();

Loading…
Cancel
Save