public void Start(bool b) { if (!Directory.Exists(TargetPath)) { throw new Exception("目标路径不明确!"); } if (!File.Exists(ModelPath)) { throw new Exception("模板未找到"); } if (SourceData == null) { throw new Exception("没有元数据"); } if (TaskRunning) { return; } TaskRunning = true; //启动线程 var failCount = 0; foreach (var queue in SourceData) { if (!runFlag) { break; } WordFile word = new WordFile( ); Debug.WriteLine("建立WordFile"); bool flag = word.ReadFile(ModelPath); //判断是否读取失败 int num = 0; //替换 if (flag) { num = word.ReplaceMark("%m", queue.GetQueue( )); } //写入文件 bool writeFlag = word.WriteFile(@TargetPath + "/" + queue.Name + ".docx"); if (!writeFlag || !flag || num == 0) { failCount++; } //通知监听器 Debug.WriteLine("Debug:位置" + (SourceData.IndexOf(queue) + 1) + " 全部数量:" + SourceData.Count + " 失败个数:" + failCount); Program.PutMessage("Debug:位置" + (SourceData.IndexOf(queue) + 1) + " 全部数量:" + SourceData.Count + " 失败个数:" + failCount); Listener?.Invoke(SourceData.IndexOf(queue) + 1, SourceData.Count, failCount); } //任务结束 TaskRunning = false; Callback?.Invoke(true); }