public void Execute(QMSetting setting) { try { stopFlag = false; setting.preExecute(queue.Count); if (!Directory.Exists(resDir)) { Directory.CreateDirectory(resDir); } taskNomber = 0; using (reportWriter = new StreamWriter(Path.Combine(resDir, "#report" + DateTime.Now.ToString("yyyyMMddHHmmssfff")) + ".txt", false, Encoding.UTF8)) { if (outMat != null && File.Exists(outMat)) { using (var matReader = new FileStream(outMat, FileMode.Open)) { outML = (List <Material>)(new DataContractJsonSerializer(typeof(List <Material>))).ReadObject(matReader); AddRepString(false, "QInf", MessageReader.GetMessage("Msg:Q:InfOMF", outML.Count)); } } AddRepString(false, "QS", MessageReader.GetMessage("Msg:Q:Start"), false); AddRepString(false, "QInf", MessageReader.GetMessage("Msg:Q:InfTC", queue.Count)); try { foreach (var ci in queue) { ++taskNomber; Calc(ci, setting); if (stopFlag) { throw new Exception(MessageReader.GetMessage("Err:UserStop")); } } AddRepString(false, "QF", MessageReader.GetMessage("Msg:Q:Fin")); } catch (Exception e) { AddRepString(true, "QErr", ErrorLogger.Log(e), false); AddRepString(false, "QEF", MessageReader.GetMessage("Msg:Q:FinError")); } } setting.postExecute(); } catch (Exception e) { ErrorLogger.Log(e); } }
static void Main(string[] args) { try { string file; if (args.Length > 0) { file = args[0]; } else { Console.WriteLine("Программа запущена без параметров."); return; } if (file == "-e") { Console.WriteLine("Файл лога ошибок:"); Console.WriteLine(ErrorLogger.FileName); return; } var qm = QueueManager.LoadFromFile(file); Console.TreatControlCAsInput = false; Console.CancelKeyPress += (_, e) => { e.Cancel = true; qm.Stop(); }; var qms = new QMSetting() { preExecute = taskCount => { Console.ForegroundColor = ConsoleColor.White; Console.BackgroundColor = ConsoleColor.Black; Console.CursorVisible = false; Console.Clear(); Console.Title = "Axial2D. Всего задач: " + taskCount.ToString(); }, postExecute = () => { Console.CursorVisible = true; Console.WriteLine(); }, preCICalc = (nomber, name, startTime) => { string dt = startTime.ToString("dd.MM.yyyy HH:mm:ss.fff").PadRight(62); if (name != null) { name = name.Trim(); } else { name = "<Без названия>"; } if (name.Length > 62) { name = name.Substring(0, 59) + "..."; } if (name.Length < 62) { name = name.PadRight(62); } if (nomber == 1) { Console.WriteLine("+----+--------+---------------------------------------------------------------+"); } else { Console.WriteLine("+----+========+===============================================================+"); } Console.WriteLine("|" + nomber.ToString().PadLeft(4) + "| Задача | " + name + "|"); Console.WriteLine("+----+--------+---------------------------------------------------------------+"); Console.WriteLine(" | Запуск | " + dt + "|"); Console.WriteLine(" +----+---+----------------+----------------------------------------------+"); Console.WriteLine(" | 0%| | |"); Console.WriteLine(" +----+---+----------------+----------------------------------------------+"); Console.WriteLine(" | Статус | Выполняется |"); Console.WriteLine(" +--------+---------------------------------------------------------------+"); }, CIPCRef = (percent, time) => { Console.CursorLeft = 6; Console.CursorTop = Console.CursorTop - 4; Console.Write(percent.ToString().PadLeft(3)); Console.CursorLeft = 33; Console.Write(time.ToString("dd.MM.yyyy HH:mm:ss.fff")); Console.CursorLeft = 11; Console.Write(string.Empty.PadLeft(percent / 5, '█')); Console.CursorTop = Console.CursorTop + 4; }, CISUpd = (status) => { Console.CursorTop = Console.CursorTop - 2; Console.CursorLeft = 16; switch (status) { case QMSetting.WorkStatus.Complete: Console.ForegroundColor = ConsoleColor.DarkGreen; Console.Write("Выполнено успешно "); Console.ForegroundColor = ConsoleColor.White; break; case QMSetting.WorkStatus.Run: Console.Write("Выполняется "); break; case QMSetting.WorkStatus.Error: Console.ForegroundColor = ConsoleColor.DarkRed; Console.Write("Ошибка! "); Console.ForegroundColor = ConsoleColor.White; break; case QMSetting.WorkStatus.Lag: Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("Процесс молчит более 10 минут"); Console.ForegroundColor = ConsoleColor.White; break; } Console.CursorTop = Console.CursorTop + 2; }, postCIGood = () => { ; }, postCIError = (msg) => { //Console.CursorTop = Console.CursorTop + 2; Console.CursorLeft = 0; Console.WriteLine(" | " + msg.PadRight(71).Substring(0, 71) + "|"); Console.WriteLine(" +------------------------------------------------------------------------+"); }, postCIAny = () => { Console.CursorLeft = 0; Console.CursorTop = Console.CursorTop - 1; } }; qm.Execute(qms); } catch (Exception e) { Console.WriteLine(ErrorLogger.Log(e)); } //Console.WriteLine(); /*Console.WriteLine("Нажмите любую клавишу для выхода..."); * Console.ReadKey(true);*/ }
private void Calc(ComputeItem ci, QMSetting setting) { AddRepString(false, "TS", MessageReader.GetMessage("Msg:T:Start", taskNomber, ci.Name ?? String.Empty), false); var startTime = DateTime.Now; int percent = 0; bool percentRefreshed = true; var lastConnect = startTime; // хранит время последнего выхода процесса на связь string dname = Path.Combine(resDir, startTime.ToString("yyyyMMddHHmmssfff")); if (!string.IsNullOrWhiteSpace(ci.Name)) { dname += "[" + new string(ci.Name.Trim().Select(c => (System.IO.Path.GetInvalidFileNameChars().Contains(c)) ? '_' : c).ToArray()) + "]"; } var saver = new Saver(dname); AddRepString(false, "TDir", dname); int mfi = 1; ci.BaseMaterials = ci.BaseMaterials.ToArray(); // копия массива ci.OverheadMaterials = ci.OverheadMaterials.ToArray(); var usingmat = materials.Concat(outML).Where((_, i) => { var bi = ci.BaseMaterials.ToList().BinarySearch(i + 1); var oi = ci.OverheadMaterials.ToList().BinarySearch(i + 1); if (bi >= 0) { ci.BaseMaterials[bi] = mfi; } if (oi >= 0) { ci.OverheadMaterials[oi] = mfi; } if (oi >= 0 || bi >= 0) { ++mfi; return(true); } return(false); }).ToList(); { var sqm = new QueueManager(); sqm.materials = usingmat; sqm.queue.Add(ci); QueueManager.SaveToFile(Path.Combine(dname, taskFileName), sqm); } setting.preCICalc(taskNomber, ci.Name, startTime); var calculator = new Calculator(ci, usingmat, saver, x => { percent = x; percentRefreshed = true; lastConnect = DateTime.Now; }); bool error = false; string msg = ""; try { Task calc = new Task(calculator.Calculate); calc.Start(); while (!calc.IsCompleted) { calc.Wait(2000, new CancellationToken(stopFlag)); if (calc.IsFaulted) { throw calc.Exception; } if (percentRefreshed) { percentRefreshed = false; setting.CIPCRef((byte)percent, lastConnect); setting.CISUpd(QMSetting.WorkStatus.Run); } if (DateTime.Now.Subtract(lastConnect).TotalMinutes > 10) { setting.CISUpd(QMSetting.WorkStatus.Lag); } } } catch (Exception e) { error = true; msg = ErrorLogger.Log(e); } ((IDisposable)saver).Dispose(); if (error) { setting.CISUpd(QMSetting.WorkStatus.Error); setting.postCIError(msg); AddRepString(true, "TErr", msg, false); AddRepString(false, "TEF", MessageReader.GetMessage("Msg:T:FinError", taskNomber, percent), false); } else { setting.CISUpd(QMSetting.WorkStatus.Complete); setting.postCIGood(); AddRepString(false, "TF", MessageReader.GetMessage("Msg:T:Fin", taskNomber), false); } setting.postCIAny(); }