Beispiel #1
0
        /// <summary>
        /// 开始Job操作
        /// </summary>
        /// <param name="name"></param>
        public async static void Start(string name)
        {
            var currentJobInfo = DataManager.Current.JobInfos.First(c => c.Name == name);

            if (currentJobInfo.Status == JobStatusType.Stop)
            {
                JobManager.GetSchedule(name).Enable();
                JobInfoManager.Refresh();
            }
            else if (currentJobInfo.Status == JobStatusType.Obsolete)
            {
                var clickresult = await DialogManager.ShowMessageAsync(Application.Current.MainWindow as MetroWindow, "务已经过期", "是否重新建立新的任务(保持原设置)", MessageDialogStyle.AffirmativeAndNegative);

                if (clickresult == MessageDialogResult.Negative)
                {
                    return;
                }
                JobInfoManager.RemoveJob(currentJobInfo);

                var isSuccess = RunSchedule(currentJobInfo.ScheduleInfo);
                if (isSuccess)
                {
                    var isCreateJobSuccess = JobInfoManager.CreateJob(currentJobInfo.ScheduleInfo);
                    if (isCreateJobSuccess)
                    {
                        //MessageBox.Show("任务启用成功");
                        JobInfoManager.Refresh();
                        Messenger.Default.Send <string>("", MessengerToken.CLOSEWINDOW);
                    }
                    else
                    {
                        MessageBox.Show("任务启用失败");
                    }
                }
                else
                {
                    MessageBox.Show("任务启用失败");
                }
            }

            else
            {
                MessageBox.Show("任务已经在启用状态");
            }
        }
Beispiel #2
0
        public ProcessResult RunProcess(Action <object, OutputArgs> outputDataReceivedAction, Action <object> errorDataReceivedAction = null, Action <object> exitDataReceivedAction = null)
        {
            DateTime Starttime;
            DateTime Endtime;
            var      status = 0;
            var      p      = new Process()
            {
                StartInfo =
                {
                    FileName               = scheduleInfo.AsmPath,
                    UseShellExecute        = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    CreateNoWindow         = true,
                    Arguments              = scheduleInfo.Name,
                }
            };

            if (DataManager.Current.RunningJob.Contains(scheduleInfo.Name))
            {
                return(new ProcessResult()
                {
                    Status = status,
                    Name = scheduleInfo.Name
                });
            }
            DataManager.Current.RunningJob.Add(scheduleInfo.Name);
            JobInfoManager.Refresh();
            p.Start();
            Starttime = p.StartTime;

            p.OutputDataReceived += (o, e) =>
            {
                if (!string.IsNullOrEmpty(e.Data) && status == 0)
                {
                    var arg = new OutputArgs(e.Data, scheduleInfo.Name);
                    outputDataReceivedAction(e, arg);
                }
            };

            p.ErrorDataReceived += (o, e) =>
            {
                if (!string.IsNullOrEmpty(e.Data) && status == 0)
                {
                    var msg = ("--------------------程序异常!--------------------\n");
                    var arg = new OutputArgs(msg + e.Data, scheduleInfo.Name);
                    outputDataReceivedAction(e, arg);
                    p.EnableRaisingEvents = false;

                    status = -1;
                }
            };

            //当EnableRaisingEvents为true,进程退出时Process会调用下面的委托函数
            p.Exited += (o, e) =>
            {
                if (status == 0)
                {
                    var msg = ("--------------------程序执行完毕!--------------------\n");
                    var arg = new OutputArgs(msg, scheduleInfo.Name);
                    outputDataReceivedAction(e, arg);

                    status = 1;
                }
            };
            p.EnableRaisingEvents = true;
            p.BeginOutputReadLine();
            p.BeginErrorReadLine();
            p.WaitForExit();
            Endtime = p.ExitTime;
            p.Close();
            DataManager.Current.RunningJob.Remove(scheduleInfo.Name);

            switch (status)
            {
            case -1:
                Task.Run(() =>
                {
                    errorDataReceivedAction?.Invoke(scheduleInfo);
                });

                break;

            case 1:
                Task.Run(() =>
                {
                    exitDataReceivedAction?.Invoke(scheduleInfo);
                });
                break;

            case 0: break;

            default:
                break;
            }

            return(new ProcessResult()
            {
                Status = status,
                Endtime = Endtime,
                Starttime = Starttime,
                Name = scheduleInfo.Name
            });
        }