public void BatchProcess(string inputDir, string outputDir) { DirectoryInfo folder = new DirectoryInfo(inputDir); int n = 0; int total = folder.GetFiles("*.arr").Length; int step = (int)Math.Ceiling((double)folder.GetFiles("*.arr").Length / 10000); Console.Write("进度 000.00%"); foreach (FileInfo f in folder.GetFiles("*.arr")) { FileInfo file = f;//防止在VS2010下报错 PrimalArrivalList pal = new PrimalArrivalList(); pal.ReadFromArrFile(file.FullName); Task ta = factory.StartNew(() => { SellingRecordList Srlist; ControlRecordList Crlist; Process(pal, out Srlist, out Crlist); //判断文件路径是否存在,不存在则创建文件夹 if (!System.IO.Directory.Exists(outputDir + @"\Sell")) { System.IO.Directory.CreateDirectory(outputDir + @"\Sell");//不存在就创建目录 } Srlist.WriteToFile(outputDir + @"\Sell\" + pal.PAListID + ".sr"); if (!System.IO.Directory.Exists(outputDir + @"\Control")) { System.IO.Directory.CreateDirectory(outputDir + @"\Control");//不存在就创建目录 } Crlist.WriteToFile(outputDir + @"\Control\" + pal.PAListID + ".cr"); if (n++ % step == 0) { lock (folder) { Console.SetCursorPosition(Console.CursorLeft - 7, Console.CursorTop); Console.Write("{0}%", String.Format("{0:000.00}", Math.Round(((double)n / (double)total), 4) * 100)); } } }, cts.Token); tasks.Add(ta); } Task.WaitAll(tasks.ToArray()); tasks.Clear(); Console.SetCursorPosition(Console.CursorLeft - 7, Console.CursorTop); Console.WriteLine("100.00 % 完成!"); }
public void Dowork(string arrpath, string srpath, string ctlpath, string outpupath) { DirectoryInfo arrFolder = new DirectoryInfo(arrpath); DirectoryInfo srFolder = new DirectoryInfo(srpath); DirectoryInfo ctlFolder = new DirectoryInfo(ctlpath); FileInfo[] sr = srFolder.GetFiles(); FileInfo[] ctl = ctlFolder.GetFiles(); Dictionary <SimStatic, double> dic = new Dictionary <SimStatic, double>(); Dictionary <SimStatic, List <double> > listDic = new Dictionary <SimStatic, List <double> >(); foreach (SimStatic s in IndexList) { if (s.Cal != null) { dic.Add(s, 0); } else if (s.TCal != null) { listDic.Add(s, new List <double>()); } } int n = 0; int total = arrFolder.GetFiles("*.arr").Length; int step = (int)Math.Ceiling((double)arrFolder.GetFiles("*.arr").Length / 10000); Console.Write("进度 000.00%"); foreach (FileInfo file in arrFolder.GetFiles("*.arr")) { Task ta = factory.StartNew(() => { PrimalArrivalList pal = new PrimalArrivalList(); pal.ReadFromArrFile(file.FullName); FileInfo srFile = sr.FirstOrDefault(i => i.Name == file.Name.Substring(0, file.Name.IndexOf('.')) + ".sr"); FileInfo ctlFile = ctl.FirstOrDefault(i => i.Name == file.Name.Substring(0, file.Name.IndexOf('.')) + ".cr"); ControlRecordList crl = GenCrl(ctlFile, pal); SellingRecordList srl = GenSrl(srFile, pal); lock (dic) { foreach (SimStatic s in IndexList) { if (s.Cal != null) { dic[s] += s.Cal(pal, srl, crl); } else if (s.TCal != null) { List <double> temp = s.TCal(pal, srl, crl); for (int i = 0; i < temp.Count; i++) { if (listDic[s].Count <= i) { listDic[s].Add(0); } listDic[s][i] += temp[i]; } } } } if (n++ % step == 0) { lock (arrFolder) { Console.SetCursorPosition(Console.CursorLeft - 7, Console.CursorTop); Console.Write("{0}%", String.Format("{0:000.00}", Math.Round(((double)n / (double)total), 4) * 100)); } } }, cts.Token); tasks.Add(ta); } Task.WaitAll(tasks.ToArray()); tasks.Clear(); Console.SetCursorPosition(Console.CursorLeft - 7, Console.CursorTop); Console.WriteLine("100.00 % 完成!"); //输出 StreamWriter sw = new StreamWriter(outpupath, true); sw.Write("\r\n" + arrFolder.FullName + ","); foreach (SimStatic s in IndexList) { if (s.Cal != null) { if (s.IsAvg) { dic[s] = dic[s] / (double)total; } if (IndexList.IndexOf(s) < IndexList.Count - 1) { sw.Write("{0},", dic[s]); } else { sw.Write("{0}", dic[s]); } } else if (s.TCal != null) { if (s.IsAvg) { for (int i = 0; i < listDic[s].Count; i++) { listDic[s][i] = listDic[s][i] / (double)total; } } sw.WriteLine(); sw.WriteLine(s.Name); for (int i = 0; i < listDic[s].Count; i++) { sw.WriteLine("{0},", listDic[s][i]); } } } sw.Close(); }