Exemple #1
0
        static Task CreatTask(BaseJob tmodel, DateTime currentTime)
        {
            var task1 = new Task(() =>
            {
                string slog = "任务进程(" + currentTime.ToString("yyyy-MM-dd :HH:mm:ss") + "):" + tmodel.tableConfig.FolderName + "->" + tmodel.tableConfig.FileName;
                if (tmodel.LastDateTime != DateTime.MinValue && Convert.ToDateTime(currentTime.ToString("yyyy-MM-dd HH:mm:ss")) < Convert.ToDateTime((tmodel.LastDateTime.AddSeconds(tmodel.Task_Fre)).ToString("yyyy-MM-dd HH:mm:ss")))
                {
                    //   GlobalObject.RichTextLog.AppendTextByAsync(string.Format("{0},上次时间戳{1},本次时间戳{2}", slog, tmodel.LastDateTime, currentTime), Color.Brown);
                    tmodel.IsTaskBusy = false;
                    return;
                }
                DateTime startWatch = DateTime.Now;
                try
                {
                    string delayedString = null;
                    if (tmodel.Task_DelayedTime != 0)
                    {
                        delayedString = string.Format("延迟执行时间为: {0} ", tmodel.Task_DelayedTime);
                    }
                    GlobalObject.RichTextLog.AppendTextByAsync(slog + " 启动! " + delayedString, Color.Blue);
                    //Log.WriteLine(slog + "启动!", GlobalObject.RunFolderName);
                    tmodel.RunTask(currentTime);
                }
                catch (Exception ex)
                {
                    tmodel.RunTaskException(currentTime, ex);
                }
                finally
                {
                    tmodel.LastDateTime = currentTime;
                    TimeSpan ts         = DateTime.Now - startWatch;
                    slog += string.Format(" 耗时{0}ms 结束!\n", ts.TotalMilliseconds);
                    GlobalObject.RichTextLog.AppendTextByAsync(slog, Color.Blue);
                    //Log.WriteLine(slog, GlobalObject.RunFolderName);
                    tmodel.IsTaskBusy = false;
                }
            });

            return(task1);
        }