protected override void OnClosing(CancelEventArgs e) { //通知UniqueAsyncProcess结束任务 UniqueAsyncProcess.RequireEnd(); //询问是否返回数据 if (textBox2.Text.Length > 0) { DialogResult = MessageBox.Show("是否应用标注?", Text, MessageBoxButtons.YesNoCancel); switch (DialogResult) { case DialogResult.Cancel: e.Cancel = true; break; case DialogResult.Yes: try { string[] patterns = RegexText.GetLines(); MatchCollection matches = Regex.Matches(FileContext, patterns[0]); Annotations = GetAnnoFromRegex(brushListPanel.CurrentItem, matches.ToStringArray(), patterns.SubArray(1, patterns.Length - 1), brushListPanel.CurrentItem.AnnoType.GetFields(FieldsOrder.BaseToSub), progress: new GlobalMessage.Progress() { Print = PrintStatus, ProgressingFormatString = "计算中,完成{0}%" }); } catch (Exception ex) { MessageBox.Show("正则匹配错误:" + ex.Message, ex.Source); e.Cancel = true; } break; case DialogResult.No: break; } } base.OnClosing(e); }
/// <summary> /// 计算正则表达式并写入textBox3中 /// </summary> /// <param name="abort"></param> private void CalRegex(Process.Abort abort) { try { if (textBox2.Text.Length > 0) { //显示的最大匹配数 const int max = 100; //报告相关 GlobalMessage.Add("status", "计算正则..."); string[] patterns = RegexText.GetLines(); MatchCollection matches = Regex.Matches(FileContext, patterns[0]); StringBuilder matchResult = new StringBuilder(""); int realCount = Math.Min(max, matches.Count); //报告相关 GlobalMessage.Progress progress = new GlobalMessage.Progress(realCount) { ProgressedString = matches.Count > max ? $"就绪 {realCount}/{matches.Count}个匹配项" : $"就绪 {matches.Count}个匹配项" }; if (matches.Count > max) { GlobalMessage.Add("info delay", $"匹配项过多,加载前{max}项"); } ListViewGroup[] groups = new ListViewGroup[realCount]; for (int i = 0; i < realCount; i++) { if (abort != null && abort()) { throw new Process.ProcessAbortException(); } matchResult.Append(matches[i].Value.Replace("\n", @"\n") + "\r\n"); FieldInfo[] fields = brushListPanel.CurrentItem.AnnoType.GetFields(FieldsOrder.BaseToSub); AnnotationBase[] annos = GetAnnoFromRegex(brushListPanel.CurrentItem, new string[1] { matches[i].Value }, patterns.SubArray(1, patterns.Length - 1), fields, abort); if (annos == null) { throw new Process.ProcessAbortException(); } ListViewGroup group = new ListViewGroup($"Match {i} ({annos.Length})"); foreach (AnnotationBase anno in annos) { object[] values = anno.GetFieldsValues(fields); group.Items.Add(new ListViewItem(values.ToStringArray())); } groups[i] = group; //报告相关 progress.Report(i + 1); } //报告相关 if (realCount == 0) { progress.Report(progress.MaxValue); } Invoke(new Action(() => { textBox3.Text = matchResult.ToString().Substring(0, Math.Min(textBox3.MaxLength, matchResult.Length)); listView1.Groups.AddRange(groups); foreach (ListViewGroup group in groups) { listView1.Items.AddRange(group.Items); } })); } else { GlobalMessage.Add("status", "就绪"); } } catch (Process.ProcessAbortException) { } catch (Exception ex) { GlobalMessage.Add("status", "正则计算错误"); GlobalMessage.Add("exception delay", ex.Message); } finally { GC.Collect(); } }