Browse Source

handleSearchBtn_Click, fillChildNode, buildResultTree теперь async. Создан TaskController для управления текущей задачей. Реализован ProgressReportModel для IProgress и последующего вывода информации из потока на форму. fillChildNode облагорожен, исправлена жившая в нем ошибка

release
Никита 6 years ago
parent
commit
fb01ceac2a
  1. 88
      fileFinder/MainForm.cs
  2. 14
      fileFinder/ProgressReportModel.cs
  3. 51
      fileFinder/TaskController.cs
  4. 2
      fileFinder/fileFinder.csproj

88
fileFinder/MainForm.cs

@ -8,11 +8,15 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using System.IO; using System.IO;
using System.Threading;
namespace fileFinder namespace fileFinder
{ {
public partial class MainForm : Form public partial class MainForm : Form
{ {
bool hintHided = false;
TaskController mainController = new TaskController();
public MainForm() public MainForm()
{ {
InitializeComponent(); InitializeComponent();
@ -43,7 +47,6 @@ namespace fileFinder
} }
} }
bool hintHided = false;
private void curDirTextBox_Enter(object sender, EventArgs e) private void curDirTextBox_Enter(object sender, EventArgs e)
{ {
if (!hintHided) if (!hintHided)
@ -52,48 +55,77 @@ namespace fileFinder
curDirTextBox.Text = ""; curDirTextBox.Text = "";
} }
} }
private void handleSearchBtn_Click(object sender, EventArgs e) private async Task<TreeNode> buildResultTree(TaskController controller, IProgress<ProgressReportModel> report)
{ {
resultViewer.Nodes.Clear();
List<String> foundFiles = new List<String>();
TreeNode itemsNode = new TreeNode(); TreeNode itemsNode = new TreeNode();
List<String> foundFiles = new List<String>();
ProgressReportModel _report = new ProgressReportModel();
try try
{ {
foundFiles = Directory.GetFiles(curDirTextBox.Text, queryTextBox.Text, SearchOption.AllDirectories).ToList<String>(); foundFiles = Directory.GetFiles(curDirTextBox.Text, queryTextBox.Text, SearchOption.AllDirectories).ToList<String>();
} catch { } }
catch (Exception ex)
{
var dialog = new ThreadExceptionDialog(ex);
dialog.ShowDialog();
}
int counter = 0;
foreach (String item in foundFiles) foreach (String item in foundFiles)
fillChildNode(itemsNode, item.Replace(curDirTextBox.Text, "")); {
while (controller.isPaused)
Thread.Sleep(100);
await fillChildNode(itemsNode, item.Replace(curDirTextBox.Text, ""));
_report.progress = counter;
_report.currentFileUrl = item;
report.Report(_report);
}
controller.stopTask();
return itemsNode;
}
resultViewer.Nodes.Add(itemsNode); private void reportProgress(object sender, ProgressReportModel report)
{
infoLabel.Text = report.currentFileUrl;
} }
public void fillChildNode (TreeNode node, String item) private async void handleSearchBtn_Click(object sender, EventArgs e)
{
Progress<ProgressReportModel> progress = new Progress<ProgressReportModel>();
progress.ProgressChanged += reportProgress;
TreeNode tN = await Task.Run(() => buildResultTree(mainController, progress));
resultViewer.Nodes.Clear();
resultViewer.Nodes.Add(tN);
}
public async Task fillChildNode (TreeNode node, String item)
{ {
int backSlashIndex = item.IndexOf("\\"); int backSlashIndex = item.IndexOf("\\");
if (backSlashIndex > -1) switch (backSlashIndex)
{ {
if (backSlashIndex == 0) case -1:
{ node.Nodes.Add(item, item, 2, 2);
break;
case 0:
item = item.Remove(0, 1); item = item.Remove(0, 1);
backSlashIndex = item.IndexOf("\\"); await fillChildNode(node, item);
} break;
String currentNodeName = item.Substring(0, backSlashIndex); default:
int nodeIndex = node.Nodes.IndexOfKey(currentNodeName); String currentNodeName = item.Substring(0, backSlashIndex);
if (nodeIndex != -1) int nodeIndex = node.Nodes.IndexOfKey(currentNodeName);
fillChildNode(node.Nodes[nodeIndex], item.Remove(0, backSlashIndex + 1));
else
{
node.Nodes.Add(currentNodeName, currentNodeName, 0, 0);
nodeIndex = node.Nodes.IndexOfKey(currentNodeName);
fillChildNode(node.Nodes[nodeIndex], item.Remove(0, backSlashIndex + 1));
}
} else
node.Nodes.Add(item, item, 2, 2);
}
if (nodeIndex != -1)
await fillChildNode(node.Nodes[nodeIndex], item.Remove(0, backSlashIndex + 1));
else
{
node.Nodes.Add(currentNodeName, currentNodeName, 0, 0);
nodeIndex = node.Nodes.IndexOfKey(currentNodeName);
await fillChildNode(node.Nodes[nodeIndex], item.Remove(0, backSlashIndex + 1));
}
break;
}
}
} }
} }

14
fileFinder/ProgressReportModel.cs

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace fileFinder
{
class ProgressReportModel
{
public int progress { get; set; }
public string currentFileUrl { get; set; }
}
}

51
fileFinder/TaskController.cs

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace fileFinder
{
class TaskController
{
public bool isPaused { get; private set; }
public bool isFinished { get; private set; }
public TaskController ()
{
isPaused = false;
isFinished = false;
}
public bool pauseTask()
{
if (!isPaused && !isFinished)
{
isPaused = true;
return true;
} else
return false;
}
public bool resumeTask ()
{
if (isPaused && !isFinished)
{
isPaused = false;
return true;
} else
return false;
}
public bool stopTask ()
{
if (!isFinished)
{
isFinished = true;
return true;
} else
return false;
}
}
}

2
fileFinder/fileFinder.csproj

@ -52,7 +52,9 @@
<DependentUpon>MainForm.cs</DependentUpon> <DependentUpon>MainForm.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="ProgressReportModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TaskController.cs" />
<EmbeddedResource Include="MainForm.resx"> <EmbeddedResource Include="MainForm.resx">
<DependentUpon>MainForm.cs</DependentUpon> <DependentUpon>MainForm.cs</DependentUpon>
</EmbeddedResource> </EmbeddedResource>

Loading…
Cancel
Save