V
V
Vladimir Karpenko2017-10-15 01:26:32
ASP.NET
Vladimir Karpenko, 2017-10-15 01:26:32

I am writing a task manager in ASP.NET. How to implement task execution time calculation?

How to organize the calculation of task execution time?
Here is my model:

public class Task
    {
        public int Id { get; set; }
        public int? ParentId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public TaskStatus TaskStatus { get; set; }
        public TimeSpan ActualPerformanceTime { get; set; } //время выполнения задачи
    }

    public enum TaskStatus
    {
        Appointed, //зарегистрирована в системе
        CarriedOut, //выполняется
        Suspended, //приостановлена
        Completed, //завершена
    }

So far, only the thought came to mind to add a new property to the model. Then use the following function to calculate the execution time at a certain moment:public DateTime StartPerformanceDate { get; set; }
public TimeSpan CalcActualPerformanceTime(TaskManagerContext dbcontext, Models.Task task)
        {
            var targetTask = task;      
            //Если задача не выполняется, вернуть последнее вычисленное время выполнения
            if (targetTask.TaskStatus!= Models.TaskStatus.CarriedOut)
                return targetTask.ActualPerformanceTime;
            //Иначе вычислить время выполнения задачи на данный момент
            var tempPerformanceTime = DateTime.Now - task.StartPerformanceDate;
            targetTask.ActualPerformanceTime += tempPerformanceTime;
            targetTask.StartPerformanceDate = DateTime.Now;
            dbcontext.Update(targetTask);
            return targetTask.ActualPerformanceTime;
        }

To stop, continue execution, and complete a task, use the following functions:
public void PauseTask(TaskManagerContext dbcontext, Models.Task task)
        {
            var targetTask = task;
            CalcActualPerformanceTime(dbcontext, targetTask);
            targetTask.TaskStatus = Models.TaskStatus.Suspended;
            dbcontext.Update(targetTask);
        }

        public void StartTask(TaskManagerContext dbcontext, Models.Task task)
        {
            var targetTask = task;
            targetTask.TaskStatus = Models.TaskStatus.CarriedOut;
            targetTask.StartPerformanceDate = DateTime.Now;
            dbcontext.Update(targetTask);
        }

        public void FinishTask(TaskManagerContext dbcontext, Models.Task task)
        {
            var targetTask = task;
            targetTask.TaskStatus = Models.TaskStatus.Completed;
            CalcActualPerformanceTime(dbcontext, targetTask);
            dbcontext.Update(targetTask);
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
eRKa, 2017-10-15
@Zeedjator

Stopwatch is usually used to measure the running time .
In general, the question is not entirely clear if you seem to have organized everything. What do you not like?

C
cicatrix, 2017-10-15
@cicatrix

You generally read about the lifetime of the process in asp.net. The fact is that I would not rely on the execution of any background tasks in an application that IIS can restart at any time. If you need a server application that runs independently of the web face, it is better to write it as a separate application and host it on the server as a separate task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question