Answer the question
In order to leave comments, you need to log in
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, //завершена
}
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;
}
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
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?
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 questionAsk a Question
731 491 924 answers to any question