From 09cea16cc1b704c1163d5bcc8c69bfa9b7f9bce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Tue, 7 Sep 2021 22:37:01 +0800 Subject: [PATCH] =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=8F=90=E7=A4=BA=E5=92=8C?= =?UTF-8?q?=E7=B4=A2=E5=BC=95=E6=9B=B4=E6=96=B0=E8=BF=9B=E5=BA=A6=E6=9D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/Index/LuceneIndexCore.cs | 23 +++++++++++++------- TextLocator/MainWindow.xaml | 12 ++++++----- TextLocator/MainWindow.xaml.cs | 29 +++++++++++++++++++------- TextLocator/Properties/AssemblyInfo.cs | 2 +- TextLocator/Util/FileUtil.cs | 19 +---------------- 5 files changed, 46 insertions(+), 39 deletions(-) diff --git a/TextLocator/Index/LuceneIndexCore.cs b/TextLocator/Index/LuceneIndexCore.cs index b4ba243..91c71e5 100644 --- a/TextLocator/Index/LuceneIndexCore.cs +++ b/TextLocator/Index/LuceneIndexCore.cs @@ -24,8 +24,9 @@ namespace TextLocator.Index /// /// 状态回调委托 /// - /// - public delegate void Callback(string status); + /// + /// + public delegate void Callback(string msg, double percent); /// /// 锁 @@ -57,10 +58,13 @@ namespace TextLocator.Index // 文件总数 int count = filePaths.Count(); + // 每次初始化的时候完成数量都是0 + finishCount = 0; + using (var countDown = new MutipleThreadResetEvent(count)) { // 设置线程最大数量 - ThreadPool.SetMaxThreads(24, 48); + ThreadPool.SetMaxThreads(32, 64); // 遍历读取文件,并创建索引 for (int i = 0; i < count; i++) { @@ -77,6 +81,7 @@ namespace TextLocator.Index // 等待所有线程结束 countDown.WaitAll(); + // 销毁 countDown.Dispose(); } @@ -167,14 +172,12 @@ namespace TextLocator.Index indexWriter.AddDocument(doc); // 优化索引 indexWriter.Optimize(); - - finishCount++; } - string msg = "索引:[" + finishCount + " / " + taskInfo.TotalCount + "] => 文件:" + filePath + ",耗时:" + (DateTime.Now - beginMark).TotalSeconds + "秒"; + string msg = "索引:[" + finishCount * 1.0F + "/" + taskInfo.TotalCount + "] => 文件:" + filePath + ",耗时:" + (DateTime.Now - beginMark).TotalSeconds + "秒"; // 执行状态回调 - taskInfo.Callback(msg); + taskInfo.Callback(msg, finishCount * 1.00F / taskInfo.TotalCount * 1.00F * 100.00F); log.Debug(msg); } @@ -184,6 +187,12 @@ namespace TextLocator.Index } finally { + + lock (locker) + { + finishCount++; + } + // 手动GC GC.Collect(); GC.WaitForPendingFinalizers(); diff --git a/TextLocator/MainWindow.xaml b/TextLocator/MainWindow.xaml index 88d078e..100b16e 100644 --- a/TextLocator/MainWindow.xaml +++ b/TextLocator/MainWindow.xaml @@ -226,11 +226,13 @@ - - - + + + + + diff --git a/TextLocator/MainWindow.xaml.cs b/TextLocator/MainWindow.xaml.cs index e856dde..0d2d271 100644 --- a/TextLocator/MainWindow.xaml.cs +++ b/TextLocator/MainWindow.xaml.cs @@ -146,14 +146,18 @@ namespace TextLocator Task.Factory.StartNew(() => { DateTime beginMark = DateTime.Now; + // 定义文件列表 + List filePaths = new List(); foreach (string s in _IndexFolders) { + log.Debug("目录:" + s); // 获取文件信息列表 - List filePaths = FileUtil.GetAllFiles(s); - // 创建索引方法 - LuceneIndexCore.CreateIndex(filePaths, rebuild, ShowStatus); + FileUtil.GetAllFiles(s, filePaths); } + // 创建索引方法 + LuceneIndexCore.CreateIndex(filePaths, rebuild, ShowStatus); + string msg = "索引执行结束,共用时:" + (DateTime.Now - beginMark).TotalSeconds + "秒"; // 显示状态 @@ -173,10 +177,15 @@ namespace TextLocator /// 显示状态 /// /// - private void ShowStatus(string text) + /// + private void ShowStatus(string text, double percent = 100) { this.Dispatcher.BeginInvoke(new Action(() => { this.WorkStatus.Text = text; + if (percent > 0) + { + this.WorkProgress.Value = percent; + } })); } @@ -311,11 +320,11 @@ namespace TextLocator resultNum++; } - string message = "检索完成!共检索到" + resultNum + "个符合条件的结果(只显示前" + num + "条)。耗时:" + (DateTime.Now - beginMark).TotalSeconds + "秒。"; + string msg = "检索完成!共检索到" + resultNum + "个符合条件的结果(只显示前" + num + "条)。耗时:" + (DateTime.Now - beginMark).TotalSeconds + "秒。"; - Message.ShowSuccess("MessageContainer", message); + Message.ShowSuccess("MessageContainer", msg); - ShowStatus(message); + ShowStatus(msg); } finally { @@ -474,6 +483,8 @@ namespace TextLocator return; } + ShowStatus("开始更新索引..."); + BuildIndex(false); } @@ -498,7 +509,9 @@ namespace TextLocator Message.ShowWarning("MessageContainer", "索引构建中,请稍等。"); return; } - build = true; + build = true; + + ShowStatus("开始重建索引..."); BuildIndex(true); } diff --git a/TextLocator/Properties/AssemblyInfo.cs b/TextLocator/Properties/AssemblyInfo.cs index 61c5dc0..f1dfc95 100644 --- a/TextLocator/Properties/AssemblyInfo.cs +++ b/TextLocator/Properties/AssemblyInfo.cs @@ -50,7 +50,7 @@ using System.Windows; //通过使用 "*",如下所示: // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.1.0.0")] -[assembly: AssemblyFileVersion("1.0.0.6")] +[assembly: AssemblyFileVersion("1.0.0.8")] // log4net [assembly: log4net.Config.XmlConfigurator(Watch = true)] \ No newline at end of file diff --git a/TextLocator/Util/FileUtil.cs b/TextLocator/Util/FileUtil.cs index 0c7dfc7..58a32c3 100644 --- a/TextLocator/Util/FileUtil.cs +++ b/TextLocator/Util/FileUtil.cs @@ -72,23 +72,6 @@ namespace TextLocator.Util return bi; } - /// - /// 获取全部文件 - /// - /// 根目录路径 - /// - public static List GetAllFiles(string rootPath) - { - log.Debug("根目录:" + rootPath); - // 声明一个files包,用来存储遍历出的word文档 - List filePaths = new List(); - // 获取全部文件列表 - GetAllFiles(rootPath, filePaths); - - // 返回文件列表 - return filePaths; - } - /// /// 获取文件大小友好显示 /// @@ -136,7 +119,7 @@ namespace TextLocator.Util /// /// 根目录路径 /// 文档列表 - private static void GetAllFiles(string rootPath, List filePaths) + public static void GetAllFiles(string rootPath, List filePaths) { DirectoryInfo dir = new DirectoryInfo(rootPath); // 得到所有子目录 -- Gitee