2
0
Tranga/TrangaTask.cs
glax 4b0a1c0a9d Added Structs Chapter and Publication
Added TaskManager and TrangaTask

TaskManager manages all TrangaTasks and starts Tasks when necessary.
Execution is necessary when time elapsed between last execution and now is greater than TrangaTask.reoccurrence.
2023-05-17 23:23:01 +02:00

49 lines
1.3 KiB
C#

namespace Tranga;
public class TrangaTask
{
private readonly Action _taskAction;
private Task? _task;
public bool lastExecutedSuccessfully => _task is not null && _task.IsCompleted;
public TimeSpan reoccurrence { get; }
public DateTime lastExecuted { get; private set; }
public TrangaTask(Action taskAction, TimeSpan reoccurrence)
{
this._taskAction = taskAction;
this.reoccurrence = reoccurrence;
}
public void Abort()
{
if(_task is not null && !_task.IsCompleted)
_task.Dispose();
}
public void Execute()
{
lastExecuted = DateTime.Now;
_task = new (_taskAction);
_task.Start();
}
public static TrangaTask CreateDownloadChapterTask(Connector connector, Chapter chapter, TimeSpan reoccurrence)
{
void TaskAction()
{
connector.DownloadChapter(chapter);
}
return new TrangaTask(TaskAction, reoccurrence);
}
public static TrangaTask CreateUpdateChaptersTask(Connector connector, Publication publication, TimeSpan reoccurrence)
{
throw new NotImplementedException();
}
public static TrangaTask CreateUpdatePublicationsTask(Connector connector, TimeSpan reoccurrence)
{
throw new NotImplementedException();
}
}