/// <summary> /// Implemented GetDemoFile method of the SubTask class /// </summary> /// <param name="number">Optional number to indicate several Sub-Tasks</param> /// <returns>Instance of the implemented class</returns> public override SubTask GetDemoFile(int number) { StartProgramTask t = new StartProgramTask(); t.Name = "Start-Program-Task_" + number.ToString(); t.Description = "This is sub-task " + number.ToString(); t.MainValue = @"C:\temp\apps\app" + number.ToString() + ".exe"; t.Asynchronous = true; if (number == 3) { t.Arguments.Add("PARAM_NAME_2"); t.Arguments.Add("PARAM_NAME_3"); } else { t.Arguments.Add("ARG1"); t.Arguments.Add("ARG2"); t.ArgumentIsParamName = true; t.Description = t.Description = t.Description + ". The arguments are the names of global parameters and not the actual values of the program arguments"; } return(t); }
/// <summary> /// Implemented Run method of the SubTask class /// </summary> /// <returns>True if the task was executed successfully, otherwise false</returns> public override bool Run() { try { if (string.IsNullOrEmpty(this.MainValue)) { this.Message = "No program to execute was defined"; this.StatusCode = 0x03; return(false); } StringBuilder sb = new StringBuilder(); bool executed = true; Parameter p; foreach (string arg in Arguments) { if (this.ArgumentIsParamName == true) { p = Parameter.GetParameter(arg, this.ParentTask.DisplayOutput); if (p.Valid == false) { this.Message = "The parameter with the name '" + arg + "' is not defined"; this.StatusCode = 0x04; return(false); } else { sb.Append(p.Value); } } else { sb.Append(arg); } sb.Append(" "); } string argString = sb.ToString().TrimEnd(' '); if (this.Asynchronous == false) { System.Diagnostics.Process.Start(this.MainValue, argString); this.Message = "The process " + this.MainValue + " was executed"; this.StatusCode = 0x02; return(true); } else { System.Threading.Tasks.Task result = StartProgramTask.RunAsyncronous(this.MainValue, argString); while (true) { if (result.IsCanceled || result.IsFaulted || result.IsCompleted) { if (result.IsCompleted) { executed = true; } else { executed = false; } break; } else { System.Threading.Thread.Sleep(10); } } if (executed == false) { this.Message = "The process " + this.MainValue + " could not be executed"; this.StatusCode = 0x02; return(false); } else { this.Message = "The process " + this.MainValue + " was executed"; this.StatusCode = 0x01; return(true); } } } catch (Exception e) { this.Message = "The process " + this.MainValue + " could not be executed\n" + e.Message; this.StatusCode = 0x01; return(false); } }