tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
JobHandler.cs
Go to the documentation of this file.
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4
6{
10 sealed class JobHandler : IDisposable
11 {
15 public bool Started => task != null;
16
20 public int? Progress { get; set; }
21
25 public string? Stage { get; set; }
26
30 readonly CancellationTokenSource cancellationTokenSource;
31
35 readonly Func<CancellationToken, Task<bool>> jobActivator;
36
40 Task<bool>? task;
41
46 public JobHandler(Func<CancellationToken, Task<bool>> jobActivator)
47 {
48 this.jobActivator = jobActivator ?? throw new ArgumentNullException(nameof(jobActivator));
49 cancellationTokenSource = new CancellationTokenSource();
50 }
51
53 public void Dispose() => cancellationTokenSource.Dispose();
54
60 public Task<bool> Wait(CancellationToken cancellationToken)
61 {
62 if (task == null)
63 throw new InvalidOperationException("Job not started!");
64
65 return task.WaitAsync(cancellationToken);
66 }
67
71 public void Cancel() => cancellationTokenSource.Cancel();
72
76 public void Start()
77 {
79 {
80 if (task != null)
81 throw new InvalidOperationException("Job already started");
83 }
84 }
85 }
86}
Class for pairing Tasks with CancellationTokenSources.
Definition JobHandler.cs:11
int? Progress
The progress of the job.
Definition JobHandler.cs:20
Task< bool >? task
The Task being run.
Definition JobHandler.cs:40
readonly Func< CancellationToken, Task< bool > > jobActivator
A Func<T, TResult> taking a CancellationToken and returning a Task<TResult> that the JobHandler will ...
Definition JobHandler.cs:35
Task< bool > Wait(CancellationToken cancellationToken)
Wait for task to complete.
Definition JobHandler.cs:60
bool Started
If the job has started.
Definition JobHandler.cs:15
readonly CancellationTokenSource cancellationTokenSource
The CancellationTokenSource for task.
Definition JobHandler.cs:30
JobHandler(Func< CancellationToken, Task< bool > > jobActivator)
Initializes a new instance of the JobHandler class.
Definition JobHandler.cs:46
string? Stage
The stage of the job.
Definition JobHandler.cs:25