/// <summary> /// 更新ListView中的项 /// </summary> private void UpdateFileListView(out ListViewItem[] items, ref ScoreDoc[] scoreDocs, ref int indexRecord, int times) { //GetSystemIcon getIcon = new GetSystemIcon();//获取图标 string extenName = null; //扩展名 string name = null; //文件名 string length = null; int k; items = new ListViewItem[times]; for (int j = 0; j < times; j++) { Document doc = this.indexSearcher.Doc(scoreDocs[indexRecord++].doc); name = doc.Get("Name"); length = doc.Get("Length"); //文件夹 if (length == "-1") { string[] subItem = { name, doc.Get("Directory"), "", doc.Get("LastWriteTime") }; items[j] = new ListViewItem(subItem, 0);//0为文件夹 } else { string[] subItem = { name, doc.Get("Directory"), Deal.FormatFileLength(length), doc.Get("LastWriteTime") }; k = name.LastIndexOf('.'); if (k > 0) { extenName = name.Substring(k, name.Length - k).ToLower(); //如果imageList里还没有该格式的图标,就添加 //文件夹,未知文件,mp3,jpg的图标已在主窗体创建时就加入ImageList中 if (this.form.imgIcon.Images.ContainsKey(extenName) == false) { Icon icon = GetSystemIcon.GetIconByFileType(extenName, false); if (icon != null) { CrossThreadOperateControl CrossUpdateImageIcon = delegate() { this.form.imgIcon.Images.Add(extenName, icon); }; this.form.Invoke(CrossUpdateImageIcon);//使用创建ImageList的线程调用 items[j] = new ListViewItem(subItem, extenName); } else { items[j] = new ListViewItem(subItem, 1);//1为未知文件 } } else { items[j] = new ListViewItem(subItem, extenName); } } else { items[j] = new ListViewItem(subItem, 1);//1为未知文件 } } } }
/// <summary> /// 线程回调,执行完索引线程后对主线程做的操作。 /// </summary> private void CallBack() { CrossThreadOperateControl CrossOperate = delegate() { lock (this.form) { this.form.IsIndex = false; this.form.索引ToolStripMenuItem.Enabled = true; this.form.停止更新ToolStripMenuItem.Enabled = false; } }; this.form.Invoke(CrossOperate); }
/// <summary> /// 线程执行完后回调 /// </summary> private void CallBack() { if (this.isSearchWithResult == false) { this.oldBoolQuery = (BooleanQuery)this.boolQuery.Clone(); } else { //当前为结果中搜索,保留为原始的boolQuery。 } CrossThreadOperateControl CrossOperate = delegate() { lock (this.form) { this.form.IsSearch = false; this.form.SearchResult = this.form.OrginalStatusText;//更新搜索结果 this.form.索引ToolStripMenuItem.Enabled = true; this.form.保存结果ToolStripMenuItem.Enabled = true; this.form.CurrentPage = 1; this.form.PageNums = this.pageNums; if (this.pageNums > 1) { this.form.btnPrevious.Visible = true; this.form.tsslPages.Visible = true; this.form.btnNext.Visible = true; this.form.btnPrevious.Enabled = false; this.form.btnNext.Enabled = true; this.form.tsslPages.Text = " " + this.currentPage + "/" + this.pageNums + " "; } else { this.form.btnPrevious.Visible = false; this.form.tsslPages.Visible = false; this.form.btnNext.Visible = false; } } }; this.form.Invoke(CrossOperate); }
private void UpdateListView(UpdateLVMethod method, int currentPage) { int TotalRecord = this.topDocs.scoreDocs.Length - ((this.currentPage - 1) * Static.PageItems); //总的数据项数 int EveryTimeRecord = 40; //每次批量增加的数量 int TotalTimes = TotalRecord / EveryTimeRecord; //批量添加的执行次数 int indexStart = (this.currentPage - 1) * Static.PageItems; //开始的index int indexRecord = indexStart; ScoreDoc[] scoreDocs = this.topDocs.scoreDocs; for (int i = 0; i <= TotalTimes; i++) { if (this.isStopSearch == true) { return; } ListViewItem[] items; if (i < TotalTimes) //i不是最后一次执行 { method(out items, ref scoreDocs, ref indexRecord, EveryTimeRecord); } else { int lastQuantity = TotalRecord + indexStart - indexRecord; method(out items, ref scoreDocs, ref indexRecord, lastQuantity); } CrossThreadOperateControl CrossUpdateListView = delegate() { lock (this.form.lvwFiles) { this.form.lvwFiles.BeginUpdate(); //利用AddRange批量增加,目的是减少刷屏次数,减轻不断闪烁问题。 this.form.lvwFiles.Items.AddRange(items); this.form.lvwFiles.EndUpdate(); } }; this.form.lvwFiles.Invoke(CrossUpdateListView); } }
/// <summary> /// 获取当前页码所在页的数据并更新ListView /// </summary> public void GetCurrentPage() { try { this.topDocs = this.indexSearcher.Search(boolQuery, Static.PageItems * this.currentPage); } catch (Exception) { this.NullResult(); //空结果,修改主窗的status状态。 this.CallBack(); //线程执行完,修改主窗的相关属性。 return; } UpdateLVMethod method; switch (this.searchMode) { case SearchMode.File: method = new UpdateLVMethod(this.UpdateFileListView); break; case SearchMode.MP3: method = new UpdateLVMethod(this.UpdateMP3ListView); break; default: method = new UpdateLVMethod(this.UpdateMP3ListView); break; } this.UpdateListView(method, this.currentPage); CrossThreadOperateControl CrossOperate = delegate() { this.form.IsSearch = false; this.form.索引ToolStripMenuItem.Enabled = true; this.form.保存结果ToolStripMenuItem.Enabled = true; }; this.form.Invoke(CrossOperate); }
/// <summary> /// 无结果或搜索时发生异常,修改主窗的status状态。 /// </summary> private void NullResult() { this.dtStop = DateTime.Now; //无结果时也是只有1页。 this.currentPage = 1; this.pageNums = 1; CrossThreadOperateControl CrossUpdateStatus = delegate() { lock (this.form) { this.form.IsSearch = false; ////无结果无法保存。 //this.form.保存结果ToolStripMenuItem.Enabled = false; this.form.OrginalStatusText = "0 个对象 用时:" + (this.dtStop - this.dtStart).TotalSeconds + "秒"; this.form.tsslStatus.Text = Deal.LimitStringLength(this.form.OrginalStatusText, this.form.TsslLength); } }; this.form.Invoke(CrossUpdateStatus); }
/// <summary> /// 搜索文件 /// </summary> public void SearchFile() { this.isStopSearch = false; this.dtStart = DateTime.Now;//开始时间 if (this.indexSearcher == null) { this.NullResult(); //空结果,修改主窗的status状态。 this.CallBack(); //线程执行完,修改主窗的相关属性。 return; } Query query; if (this.keyword.Name != "") { try { QueryParser parser = new QueryParser("Name", this.analyzer); query = parser.Parse(this.keyword.Name); boolQuery.Add(query, BooleanClause.Occur.MUST); } catch (Exception) { } } if (this.keyword.Directory != null && this.keyword.Directory != "") { PrefixQuery pq = new PrefixQuery(new Term("Directory", this.keyword.Directory)); boolQuery.Add(pq, BooleanClause.Occur.MUST); } if (this.keyword.Extension != null && this.keyword.Extension != "") { TermQuery termQuery = new TermQuery(new Term("Extension", this.keyword.Extension)); boolQuery.Add(termQuery, BooleanClause.Occur.MUST); } try { //搜索前Static.PageItems项 this.topDocs = this.indexSearcher.Search(boolQuery, Static.PageItems); } catch (Exception) { this.NullResult(); //空结果,修改主窗的status状态。 this.CallBack(); //线程执行完,修改主窗的相关属性。 return; } this.dtStop = DateTime.Now;//完成时间 if (this.topDocs.totalHits > 0) { CrossThreadOperateControl CrossUpdateStatus = delegate() { lock (this.form.tsslStatus) { this.form.OrginalStatusText = Deal.ToEnglishNumString(this.topDocs.totalHits) + " 个对象 用时:" + (this.dtStop - this.dtStart).TotalSeconds + "秒"; this.form.tsslStatus.Text = Deal.LimitStringLength(this.form.OrginalStatusText, this.form.TsslLength); } }; this.form.Invoke(CrossUpdateStatus); } else { this.NullResult(); //空结果,修改主窗的status状态。 this.CallBack(); //线程执行完,修改主窗的相关属性。 return; } this.pageNums = topDocs.totalHits / Static.PageItems + 1; //总的页数 this.currentPage = 1; //每次搜索时当前页数为1 UpdateLVMethod method = new UpdateLVMethod(this.UpdateFileListView); this.UpdateListView(method, this.currentPage); this.CallBack();//线程执行完,修改主窗的相关属性。 }
/// <summary> /// 用搜索的方式获取并显示艺术家和曲目数。 /// </summary> public void ShowMP3Artist() { this.isStopSearch = false; if (this.indexSearcher == null) { this.NullResult(); //空结果,修改主窗的status状态。 this.CallBack(); //线程执行完,修改主窗的相关属性。 return; } CrossThreadOperateControl CrossUpdateStatus = delegate() { lock (this.form) { this.form.OrginalStatusText = Deal.ToEnglishNumString(Static.MP3Artist.Count) + " 名艺术家 " + Deal.ToEnglishNumString(Static.MP3SongNums) + " 首曲目"; this.form.tsslStatus.Text = Deal.LimitStringLength(this.form.OrginalStatusText, this.form.TsslLength); } }; this.form.Invoke(CrossUpdateStatus); //该模式下只有1页,不用分页。 this.pageNums = 1; this.currentPage = 1; int TotalRecord; //总的数据项数 int EveryTimeRecord; //每次批量增加的数量 int TotalTimes; //批量添加的执行次数 int indexRecord = 0; if (Static.MArtistInfo == null) { Static.MArtistInfo = new List <MP3ArtistInfo>(); Term termField = new Term("ArtistStore"); TotalRecord = Static.MP3Artist.Count; //总的数据项数 EveryTimeRecord = 50; //每次批量增加的数量 TotalTimes = TotalRecord / EveryTimeRecord; //批量添加的执行次数 for (int i = 0; i <= TotalTimes; i++) { if (this.isStopSearch == true) { break; } ListViewItem[] items; if (i < TotalTimes) //i不是最后一次执行 { this.UpadateArtistListView(out items, ref indexRecord, EveryTimeRecord, ref termField); } else { int lastQuantity = TotalRecord - indexRecord; this.UpadateArtistListView(out items, ref indexRecord, lastQuantity, ref termField); } CrossThreadOperateControl CrossUpdateListView = delegate() { lock (this.form.lvwFiles) { this.form.lvwFiles.BeginUpdate(); //利用AddRange批量增加,目的是减少刷屏次数,减轻不断闪烁问题。 this.form.lvwFiles.Items.AddRange(items); this.form.lvwFiles.EndUpdate(); } }; this.form.lvwFiles.Invoke(CrossUpdateListView); } } else { TotalRecord = Static.MArtistInfo.Count; //总的数据项数 EveryTimeRecord = 500; //每次批量增加的数量 TotalTimes = TotalRecord / EveryTimeRecord; //批量添加的执行次数 for (int i = 0; i <= TotalTimes; i++) { if (this.isStopSearch == true) { break; } ListViewItem[] items; if (i < TotalTimes) //i不是最后一次执行 { this.UpadateArtistListView(out items, ref indexRecord, EveryTimeRecord); } else { int lastQuantity = TotalRecord - indexRecord; this.UpadateArtistListView(out items, ref indexRecord, lastQuantity); } CrossThreadOperateControl CrossUpdateListView = delegate() { lock (this.form.lvwFiles) { this.form.lvwFiles.BeginUpdate(); //利用AddRange批量增加,目的是减少刷屏次数,减轻不断闪烁问题。 this.form.lvwFiles.Items.AddRange(items); this.form.lvwFiles.EndUpdate(); } }; this.form.lvwFiles.Invoke(CrossUpdateListView); } } this.CallBack();//线程执行完,修改主窗的相关属性。 }
public void StartSave() { DateTime dtStart = DateTime.Now;//开始时间 this.sw = new StreamWriter(this.fullName, false); //输出保存时间 this.strLine = "保存时间:" + DateTime.Now.ToString(); this.sw.WriteLine(strLine); this.sw.WriteLine();//换行 CrossThreadOperateControl CrossOperate = delegate() { if (this.lvStatus != ListViewStatus.MP3Artist) { //输出搜索关键字 this.strLine = "关键字:" + this.form.CurrentKeyword; this.sw.WriteLine(strLine); this.sw.WriteLine();//换行 //输出搜索结果信息 this.strLine = "搜索结果:" + this.form.SearchResult; this.sw.WriteLine(strLine); this.sw.WriteLine();//换行 } else { this.strLine = "MP3索引数据库艺术家信息:"; this.sw.WriteLine(strLine); this.sw.WriteLine();//换行 //输出搜索结果信息 this.strLine = this.form.SearchResult; this.sw.WriteLine(strLine); this.sw.WriteLine();//换行 } //输出搜索结果 switch (this.lvStatus) { case ListViewStatus.File: this.OutPutFile(); break; case ListViewStatus.MP3: this.OutPutMP3(); break; case ListViewStatus.MP3Artist: this.OutPutMP3Artist(); break; } this.sw.Close(); }; this.form.lvwFiles.Invoke(CrossOperate); DateTime dtStop = DateTime.Now;//完成时间 string time = (dtStop - dtStart).TotalSeconds.ToString(); CrossThreadOperateControl CrossOperate2 = delegate() { this.saveForm.CallBack(time); }; this.saveForm.Invoke(CrossOperate2); }