Example #1
0
        public void RunTask()
        {
            PFTask             task = new PFTask();
            PFTaskHistoryEntry the  = new PFTaskHistoryEntry();

            PFTimers.Stopwatch sw = new PFTimers.Stopwatch();
            string             connectionString = string.Empty;
            string             scheduleFolder   = string.Empty;
            string             scheduleName     = string.Empty;

            try
            {
                if (_frm.chkEraseOutputBeforeEachTest.Checked)
                {
                    Program._messageLog.Clear();
                }

                if (_frm.txtTaskName.Text.Trim().Length == 0 ||
                    _frm.txtFileToRun.Text.Trim().Length == 0)
                {
                    _msg.Length = 0;
                    _msg.Append("You must specify a the name and a file to run for RunTest");
                    throw new System.Exception(_msg.ToString());
                }

                sw.Start();

                _msg.Length = 0;
                _msg.Append("Before taskProcessor.Start");
                Program._messageLog.WriteLine(_msg.ToString());

                System.Diagnostics.Process p = Process.GetCurrentProcess();
                foreach (ProcessThread th in p.Threads)
                {
                    _msg.Length = 0;
                    _msg.Append("Thread ID: ");
                    _msg.Append(th.Id.ToString());
                    Program._messageLog.WriteLine(_msg.ToString());
                }

                scheduleFolder = _scheduleDefinitionsFolder;
                scheduleName   = Path.GetFileNameWithoutExtension(_frm.txtTaskSchedule.Text.Trim()) + ".xml";
                if (_frm.optUseXmlFiles.Checked)
                {
                    connectionString = Path.Combine(scheduleFolder, scheduleName);
                }
                else if (_frm.optUseDatabase.Checked)
                {
                    connectionString = _taskDefsDbConnectionString;
                }
                else
                {
                    _msg.Length = 0;
                    _msg.Append("You must select storage type: XML Files or Database.");
                    throw new System.Exception(_msg.ToString());
                }


                task.TaskName              = _frm.txtTaskName.Text;
                task.TaskDescription       = _frm.txtTaskDescription.Text;
                task.TaskType              = PFAppUtils.PFEnumProcessor.StringToEnum <enTaskType>(_frm.cboTaskType.Text);
                task.MaxTaskHistoryEntries = Convert.ToInt32(_frm.txtMaxHistoryEntries.Text);
                if (_frm.optUseXmlFiles.Checked)
                {
                    task.Schedule = PFSchedule.LoadFromXmlFile(connectionString);
                }
                else
                {
                    task.Schedule = PFSchedule.LoadFromDatabase(connectionString, _frm.txtTaskSchedule.Text.Trim());
                }
                task.FileToRun        = _frm.txtFileToRun.Text;
                task.WorkingDirectory = _frm.txtWorkingDirectory.Text;
                string[] lines = _frm.txtArguments.Lines;
                task.Arguments              = lines;
                task.WindowStyle            = PFAppUtils.PFEnumProcessor.StringToEnum <enWindowStyle>(_frm.cboWindowStyle.Text);
                task.CreateNoWindow         = _frm.chkCreateNoWindow.Checked;
                task.UseShellExecute        = _frm.chkUseShellExecute.Checked;
                task.RedirectStandardOutput = _frm.chkRedirectStandardOutput.Checked;
                task.RedirectStandardError  = _frm.chkRedirectStandardError.Checked;

                the.TaskID             = task.ID;
                the.TaskName           = task.TaskName;
                the.ScheduledStartTime = task.Schedule.GetCurrentScheduledDateTime(DateTime.Now, DateTime.Now.AddHours(1));

                PFTaskProcessor taskProcessor = new PFTaskProcessor(task);
                taskProcessor.Start();

                _msg.Length = 0;
                _msg.Append("After taskProcessor.Start");
                Program._messageLog.WriteLine(_msg.ToString());

                System.Diagnostics.Process p1 = Process.GetCurrentProcess();
                foreach (ProcessThread th in p1.Threads)
                {
                    _msg.Length = 0;
                    _msg.Append("Thread ID: ");
                    _msg.Append(th.Id.ToString());
                    Program._messageLog.WriteLine(_msg.ToString());
                }
                _msg.Length = 0;
                _msg.Append("Main thread id:    ");
                _msg.Append(System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
                Program._messageLog.WriteLine(_msg.ToString());
                _msg.Length = 0;
                _msg.Append("Spawned thread id: ");
                _msg.Append(taskProcessor.ThreadID.ToString());
                Program._messageLog.WriteLine(_msg.ToString());

                while (taskProcessor.HasFinished == false)
                {
                    System.Threading.Thread.Sleep(1000);
                    System.Threading.Thread.Yield();
                }


                sw.Stop();

                _msg.Length = 0;
                _msg.Append("Elapsed time: ");
                _msg.Append(sw.FormattedElapsedTime);
                Program._messageLog.WriteLine(_msg.ToString());

                _msg.Length = 0;
                _msg.Append("\r\nTaskProcessor property values: \r\n");
                _msg.Append("Curr Directory:  ");
                _msg.Append(task.WorkingDirectory);
                _msg.Append("\r\n");
                _msg.Append("Command Line:    ");
                _msg.Append(task.FileToRun);
                _msg.Append(" ");
                _msg.Append(PFTextProcessor.ConvertStringArrayToString(task.Arguments));
                _msg.Append("\r\n");
                _msg.Append("StartTime:       ");
                _msg.Append(taskProcessor.StartTime.ToString("MM/dd/yyyy HH:mm:ss"));
                _msg.Append("\r\n");
                _msg.Append("FinishTime       ");
                _msg.Append(taskProcessor.FinishTime.ToString("MM/dd/yyyy HH:mm:ss"));
                _msg.Append("\r\n");
                _msg.Append("Elapsed Time:    ");
                _msg.Append(taskProcessor.ElapsedTimeFormatted);
                _msg.Append("\r\n");
                _msg.Append("Exit Code:       ");
                _msg.Append(taskProcessor.ProcessExitCode.ToString());
                _msg.Append("\r\n");
                _msg.Append("OutputMessages:  ");
                _msg.Append(taskProcessor.OutputMessages);
                _msg.Append("\r\n");
                _msg.Append("ErrorMessages:   ");
                _msg.Append(taskProcessor.ErrorMessages);
                _msg.Append("\r\n");
                Program._messageLog.WriteLine(_msg.ToString());

                //fill up and save the history entry
                the.ActualStartTime    = taskProcessor.StartTime;
                the.ActualEndTime      = taskProcessor.FinishTime;
                the.TaskOutputMessages = taskProcessor.OutputMessages;
                the.TaskErrorMessages  = taskProcessor.ErrorMessages;
                the.TaskReturnCode     = taskProcessor.ProcessExitCode;
                if (taskProcessor.ProcessExitCode == 0)
                {
                    the.TaskRunResult = enTaskRunResult.Success;
                }
                else
                {
                    the.TaskRunResult = enTaskRunResult.Failure;
                }
                if (_frm.optUseXmlFiles.Checked)
                {
                    string theFilename = _taskHistoryFolder + the.TaskName + "_" + the.ActualEndTime.ToString("_yyyyMMdd_HHmmss") + ".xml";
                    if (File.Exists(theFilename))
                    {
                        File.Delete(theFilename);
                    }
                    the.SaveToXmlFile(theFilename);
                }
                else if (_frm.optUseDatabase.Checked)
                {
                    the.SaveToDatabase(_taskDefsDbConnectionString);
                }
                else
                {
                    _msg.Length = 0;
                    _msg.Append("You must specify whether to store the history in XML files or Database.");
                    throw new System.Exception(_msg.ToString());
                }
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                Program._messageLog.WriteLine(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                ;
            }
        }
Example #2
0
        public static void InitTaskProcessorTest(MainForm frm)
        {
            PFTask             task = new PFTask();
            PFTaskHistoryEntry the  = new PFTaskHistoryEntry();

            PFTimers.Stopwatch sw = new PFTimers.Stopwatch();

            try
            {
                _msg.Length = 0;
                _msg.Append("InitTaskProcessorTest started ...\r\n");
                Program._messageLog.WriteLine(_msg.ToString());


                sw.Start();

                _msg.Length = 0;
                _msg.Append("Before taskProcessor.Start");
                Program._messageLog.WriteLine(_msg.ToString());

                System.Diagnostics.Process p = Process.GetCurrentProcess();
                foreach (ProcessThread th in p.Threads)
                {
                    _msg.Length = 0;
                    _msg.Append("Thread ID: ");
                    _msg.Append(th.Id.ToString());
                    Program._messageLog.WriteLine(_msg.ToString());
                }

                task.TaskName              = "TestTask01";
                task.TaskDescription       = "Test the task for Testprog";
                task.TaskType              = enTaskType.WindowsExecutable;
                task.FileToRun             = @"C:\Temp\TestAppFolders\FilesToRun\testbat01.bat";
                task.WorkingDirectory      = @"C:\Temp\TestAppFolders\FilesToRun";
                task.MaxTaskHistoryEntries = 15;
                task.Schedule              = PFSchedule.LoadFromXmlFile(@"C:\ProFast\Projects\ScheduleFiles\TestWeeklyOccurs.xml");

                the.TaskID             = task.ID;
                the.TaskName           = task.TaskName;
                the.ScheduledStartTime = task.Schedule.GetCurrentScheduledDateTime(DateTime.Now, DateTime.Now.AddHours(1));

                PFTaskProcessor taskProcessor = new PFTaskProcessor(task);
                taskProcessor.Start();

                _msg.Length = 0;
                _msg.Append("After taskProcessor.Start");
                Program._messageLog.WriteLine(_msg.ToString());

                System.Diagnostics.Process p1 = Process.GetCurrentProcess();
                foreach (ProcessThread th in p1.Threads)
                {
                    _msg.Length = 0;
                    _msg.Append("Thread ID: ");
                    _msg.Append(th.Id.ToString());
                    Program._messageLog.WriteLine(_msg.ToString());
                }
                _msg.Length = 0;
                _msg.Append("Main thread id:    ");
                _msg.Append(System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
                Program._messageLog.WriteLine(_msg.ToString());
                _msg.Length = 0;
                _msg.Append("Spawned thread id: ");
                _msg.Append(taskProcessor.ThreadID.ToString());
                Program._messageLog.WriteLine(_msg.ToString());

                while (taskProcessor.HasFinished == false)
                {
                    System.Threading.Thread.Sleep(1000);
                    System.Threading.Thread.Yield();
                }


                sw.Stop();

                _msg.Length = 0;
                _msg.Append("Elapsed time: ");
                _msg.Append(sw.FormattedElapsedTime);
                Program._messageLog.WriteLine(_msg.ToString());

                _msg.Length = 0;
                _msg.Append("\r\nTaskProcessor property values: \r\n");
                _msg.Append("StartTime:       ");
                _msg.Append(taskProcessor.StartTime.ToString("MM/dd/yyyy HH:mm:ss"));
                _msg.Append("\r\n");
                _msg.Append("FinishTime       ");
                _msg.Append(taskProcessor.FinishTime.ToString("MM/dd/yyyy HH:mm:ss"));
                _msg.Append("\r\n");
                _msg.Append("Elapsed Time:    ");
                _msg.Append(taskProcessor.ElapsedTimeFormatted);
                _msg.Append("\r\n");
                _msg.Append("Exit Code:       ");
                _msg.Append(taskProcessor.ProcessExitCode.ToString());
                _msg.Append("\r\n");
                _msg.Append("OutputMessages:  ");
                _msg.Append(taskProcessor.OutputMessages);
                _msg.Append("\r\n");
                _msg.Append("ErrorMessages:   ");
                _msg.Append(taskProcessor.ErrorMessages);
                _msg.Append("\r\n");
                Program._messageLog.WriteLine(_msg.ToString());

                //fill up and save the history entry
                the.ActualStartTime    = taskProcessor.StartTime;
                the.ActualEndTime      = taskProcessor.FinishTime;
                the.TaskOutputMessages = taskProcessor.OutputMessages;
                the.TaskErrorMessages  = taskProcessor.ErrorMessages;
                the.TaskReturnCode     = taskProcessor.ProcessExitCode;
                if (taskProcessor.ProcessExitCode == 0)
                {
                    the.TaskRunResult = enTaskRunResult.Success;
                }
                else
                {
                    the.TaskRunResult = enTaskRunResult.Failure;
                }
                //string theFilename = @"c:\tempObjectInstance\" + the.TaskName + "_" + the.TaskID.ToString() + "_" + the.ActualEndTime.ToString("_yyyyMMdd_HHmmss") + ".txt";
                string theFilename = @"c:\tempObjectInstance\" + the.TaskName + "_" + the.ActualEndTime.ToString("_yyyyMMdd_HHmmss") + ".txt";
                if (File.Exists(theFilename))
                {
                    File.Delete(theFilename);
                }
                the.SaveToXmlFile(theFilename);
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                Program._messageLog.WriteLine(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                _msg.Length = 0;
                _msg.Append("\r\n... InitTaskProcessorTest finished.");
                Program._messageLog.WriteLine(_msg.ToString());
            }
        }