public override bool Run() { ActionState savedState = State; State = ActionState.Running; CheckedStatus = CheckedStatus.Working; try { if (AltUtil.LogActions) { string actionLog = string.Format("Running action Id {0} (task Id {1}): Type '{2}', Output file '{3}' (Export format '{4}').", Id, _owner.Id, this.Kind, this.OutputFileName, this.ExportFormatName); AltUtil.LogMessage(actionLog); } switch (Kind) { case ActionKind.Export: return(Export()); case ActionKind.Print: return(Print()); case ActionKind.Run: return(RunProgram()); default: System.Diagnostics.Debug.Assert(false); return(false); } } catch (Exception ex) { AltUtil.ShowError(string.Format("Error running action Id {0} (task Id {1}): {2}.", Id, _owner.Id, ex.Message)); throw; } finally { State = savedState; CheckedStatus = CheckedStatus.CheckedOk; if (AltUtil.LogActions) { AltUtil.LogMessage(string.Format("Finished running action {0}.", Id)); } } }
private bool RunProgram() { string message; if (!CanRunProgram(out message)) { AltUtil.ShowError(string.Format("Cannot run program: {0}", message)); return(false); } using (Process prog = new Process()) { prog.StartInfo.FileName = _owner.FileName; prog.StartInfo.CreateNoWindow = true; prog.StartInfo.UseShellExecute = false; prog.StartInfo.RedirectStandardError = true; prog.StartInfo.RedirectStandardOutput = true; // prog.StartInfo.Arguments = ""; todo prog.ErrorDataReceived += new DataReceivedEventHandler(prog_ErrorDataReceived); prog.OutputDataReceived += new DataReceivedEventHandler(prog_OutputDataReceived); prog.Start(); prog.BeginErrorReadLine(); prog.BeginOutputReadLine(); // timeout 1 minute (todo: allow to specify timeout): int timeout = (int)(TimeSpan.TicksPerMinute / TimeSpan.TicksPerMillisecond); if (!prog.WaitForExit(timeout)) { AltUtil.ShowError(string.Format("Timeout waiting for program \"{0}\" to exit (waited {1} milliseconds).", _owner.FileName, timeout)); } else { if (AltUtil.LogActions) { AltUtil.LogMessage(string.Format("Program \"{0}\" ended, exit code {1}.", _owner.FileName, prog.ExitCode)); } } prog.Close(); } return(true); }
private void LogOutput(Process prog, DataReceivedEventArgs e, bool error) { // ignore empty data: if (string.IsNullOrEmpty(e.Data)) { return; } if (!AltUtil.LogProgramOutput) { return; } try { string msg = string.Format("Process \"{0}\" {1}: {2}", prog.ProcessName, error ? "ERROR" : "OUTPUT", e.Data); AltUtil.LogMessage(msg); } catch (Exception ex) { AltUtil.ShowError(ex.Message); } }
/// <summary> /// Runs all actions synchronously, in the current thread. /// </summary> public override void ScheduledRun(ScheduleBase runner) { System.Diagnostics.Debug.Assert(runner == _schedule); if (State != TaskState.Scheduled) { return; // this could happen if we were caught in a transitional state } try { State = TaskState.Busy; CheckedStatus = CheckedStatus.Working; if (AltUtil.LogTasks) { string taskLog = string.Format("Running task Id {0}: Type '{1}', File '{2}' (Report name '{3}').", Id, ReportKind, FileName, ReportName); AltUtil.LogMessage(taskLog); } lock (_actions.SyncRoot) foreach (ActionBase action in _actions) { action.Run(); } // unloading task ensures that reports are regenerated on each run: UnLoad(); } catch { // Note: errors if any should've been logged by action.Run, hence we just throw them on here. // AltUtil.ShowError(string.Format("Error running task Id {0}: {1}.", Id, ex.Message)); throw; } finally { CheckedStatus = CheckedStatus.CheckedOk; State = _schedule.Enabled ? TaskState.Scheduled : TaskState.Ready; if (AltUtil.LogTasks) { AltUtil.LogMessage(string.Format("Finished running task Id {0}.", Id)); } } }