/
TaskStatus.cs
55 lines (49 loc) · 1.35 KB
/
TaskStatus.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
namespace ModuleManager.Threading
{
public class TaskStatus : ITaskStatus
{
private bool isRunning = true;
private Exception exception = null;
private object lockObject = new object();
public bool IsRunning => isRunning;
public Exception Exception => exception;
public bool IsFinished
{
get
{
lock (lockObject)
{
return !isRunning && exception == null;
}
}
}
public bool IsExitedWithError
{
get
{
lock (lockObject)
{
return !isRunning && exception != null;
}
}
}
public void Finished()
{
lock (lockObject)
{
if (!isRunning) throw new InvalidOperationException("Task is not running");
isRunning = false;
}
}
public void Error(Exception exception)
{
lock(lockObject)
{
if (!isRunning) throw new InvalidOperationException("Task is not running");
this.exception = exception ?? throw new ArgumentNullException(nameof(exception));
isRunning = false;
}
}
}
}