From d5e2011976d45c98a012f1421f533591bb38b737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0?= Date: Thu, 4 Oct 2018 01:21:15 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=82=D0=B5=D0=BA=D1=83=D1=89=D0=B5=D0=B9?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B5=D0=B9=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=BE=D1=81=D0=BC=D1=8B=D1=81=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=BE=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20enum=20TaskState.=20?= =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=BD=D0=B5=D0=B8?= =?UTF-8?q?=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D1=83=D0=B5=D0=BC=D1=8B?= =?UTF-8?q?=D0=B5=20using=20*.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fileFinder/MainForm.cs | 98 +++++++++++++++++-------------- fileFinder/ProgressReportModel.cs | 7 +-- fileFinder/TaskController.cs | 85 +++++++++++---------------- 3 files changed, 88 insertions(+), 102 deletions(-) diff --git a/fileFinder/MainForm.cs b/fileFinder/MainForm.cs index b392668..a0fa060 100644 --- a/fileFinder/MainForm.cs +++ b/fileFinder/MainForm.cs @@ -1,14 +1,7 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; using System.Drawing; -using System.Linq; -using System.Text; using System.Threading.Tasks; using System.Windows.Forms; -using System.IO; -using System.Threading; using System.Timers; namespace fileFinder @@ -19,6 +12,7 @@ namespace fileFinder TaskController mainController = new TaskController(); SearchQueryModel searchQueryModel; System.Timers.Timer updateInfoTimer = new System.Timers.Timer(1000); + bool isFieldsChanged = false; public MainForm() { @@ -38,7 +32,7 @@ namespace fileFinder queryTextBox.Text = sets.fileNameQuery; searchQueryModel = mainController.updateSearchQueryModel(sets.fileUrl, sets.fileInnerQuery, sets.fileNameQuery); - mainController.isTaskChanged = false; + isFieldsChanged = false; } private void fillImagesToResultViewer() @@ -81,10 +75,15 @@ namespace fileFinder private void reportProgress(object sender, ProgressReportModel report) { - if (!mainController.isPaused) - updateInfoLabel(report.currentFileUrl, report.progress); - else if (mainController.isPaused) - updateResultViewer(report.currentTreeNode); + switch (mainController.state) + { + case TaskState.Work: + updateInfoLabel(report.currentFileUrl, report.progress); + break; + case TaskState.Pause: + updateResultViewer(report.currentTreeNode); + break; + } } private async void handleSearchBtn_Click(object sender, EventArgs e) @@ -92,45 +91,54 @@ namespace fileFinder Progress progress = new Progress(); progress.ProgressChanged += reportProgress; - if (mainController.isStopped || mainController.isTaskChanged) + if (isFieldsChanged) // если в форме изменился запрос + mainController.stopTask(); // то принудительно завершаем задание + isFieldsChanged = false; + + switch (mainController.state) { - mainController.stopTask(); - mainController = new TaskController(); - searchQueryModel = mainController.updateSearchQueryModel(curDirTextBox.Text, - queryTextBox.Text, innerQueryTextBox.Text); - handleSearchBtn.Text = "Pause Task"; - mainController.beginTask(); - changeVisibilityResultViewer(); - TreeNode tN = await Task.Run(() => mainController.buildResultTree(searchQueryModel, progress)); - if (tN != null) - { - updateResultViewer(tN); + case TaskState.Created: + searchQueryModel = mainController.updateSearchQueryModel(curDirTextBox.Text, + queryTextBox.Text, innerQueryTextBox.Text); + handleSearchBtn.Text = "Pause Task"; + mainController.beginTask(); changeVisibilityResultViewer(); - handleSearchBtn.Text = "Start Task"; - } - } else if (mainController.isPaused) - { - handleSearchBtn.Text = "Pause Task"; - mainController.resumeTask(); - changeVisibilityResultViewer(); - } else - { - handleSearchBtn.Text = "Resume Task"; - mainController.pauseTask(); - changeVisibilityResultViewer(); + TreeNode tN = await Task.Run(() => mainController.buildResultTree(searchQueryModel, progress)); + if (tN != null) + { + updateResultViewer(tN); + handleSearchBtn.Text = "Start Task"; + } + break; + case TaskState.Work: + handleSearchBtn.Text = "Resume Task"; + mainController.pauseTask(); + break; + case TaskState.Pause: + handleSearchBtn.Text = "Pause Task"; + mainController.resumeTask(); + break; + case TaskState.Finished: + mainController = new TaskController(); + goto case TaskState.Created; } + changeVisibilityResultViewer(); } private void changeVisibilityResultViewer() { - if (mainController.isStopped || mainController.isPaused) - { - waitOrPauseLabel.Visible = false; - resultViewer.Enabled = true; - } else + switch (mainController.state) { - waitOrPauseLabel.Visible = true; - resultViewer.Enabled = false; + case TaskState.Work: // если задание в работе, то блокируем ResultViewer + 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() { - if (!mainController.isTaskChanged) + if (!isFieldsChanged) { - mainController.isTaskChanged = true; + isFieldsChanged = true; handleSearchBtn.Text = "Start New Task"; } } diff --git a/fileFinder/ProgressReportModel.cs b/fileFinder/ProgressReportModel.cs index 3ed52c4..b459a40 100644 --- a/fileFinder/ProgressReportModel.cs +++ b/fileFinder/ProgressReportModel.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; +using System.Windows.Forms; namespace fileFinder { diff --git a/fileFinder/TaskController.cs b/fileFinder/TaskController.cs index 1ac8113..22988ec 100644 --- a/fileFinder/TaskController.cs +++ b/fileFinder/TaskController.cs @@ -6,63 +6,46 @@ using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; -using System.Timers; using System.Windows.Forms; namespace fileFinder { + public enum TaskState { Created, Work, Pause, Finished } 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(); + public TaskState state { get; private set; } - public TaskController () + public TaskController() { - isPaused = false; - isStopped = true; - isFormUpdatedAfterPause = true; - isTaskChanged = false; + state = TaskState.Created; } - public void beginTask () + public void beginTask() { - isPaused = false; - isStopped = false; + state = TaskState.Work; stopwatch.Restart(); } - public void pauseTask () + public void pauseTask() { - if (!isPaused && !isStopped) - { - isPaused = true; - isFormUpdatedAfterPause = false; - stopwatch.Stop(); - } + state = TaskState.Pause; + stopwatch.Stop(); } - public void resumeTask () + public void resumeTask() { - if (isPaused && !isStopped) - { - isPaused = false; - stopwatch.Start(); - } + state = TaskState.Work; + stopwatch.Start(); } - public void stopTask () + public void stopTask() { - if (!isStopped) - { - isStopped = true; - stopwatch.Stop(); - } + state = TaskState.Finished; + stopwatch.Stop(); } - public TimeSpan elapsedTime () + public TimeSpan elapsedTime() { return stopwatch.Elapsed; } @@ -77,30 +60,30 @@ namespace fileFinder int counter = 0; foreach (string item in foundFiles) { - if (controller.isStopped) - return null; - while (controller.isPaused) + switch (state) { - Thread.Sleep(100); - if (!controller.isFormUpdatedAfterPause) - { - controller.isFormUpdatedAfterPause = true; + case TaskState.Finished: + return null; + case TaskState.Pause: _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); + while (state == TaskState.Pause) + Thread.Sleep(100); + goto case TaskState.Work; + case TaskState.Work: + string[] fileLines = getFileContents(item); + _report.progress = counter; + _report.currentFileUrl = item; + report.Report(_report); - if ((fileLines.Length > 0) && (isFileContainQuery(fileLines, query.fileInnerQuery))) - { - counter++; - await fillChildNode(itemsNode, item.Replace(query.fileUrl, "")); + if ((fileLines.Length > 0) && (isFileContainQuery(fileLines, query.fileInnerQuery))) + { + counter++; + await fillChildNode(itemsNode, item.Replace(query.fileUrl, "")); + } + break; } } controller.stopTask();