Repository navigation
Expand file tree
/
Copy pathDisposableThreadObject.cs
More file actions
78 lines (65 loc) · 2.28 KB
/
Copy pathDisposableThreadObject.cs
File metadata and controls
78 lines (65 loc) · 2.28 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using ThunderDesign.Net.Threading.Extentions;
namespace ThunderDesign.Net.Threading.Objects
{
public abstract class DisposableThreadObject : ThreadObject, IDisposable
{
private CancellationTokenSource? _disposingCtsCached = null;
protected readonly string ClassNameCashed;
private bool _disposed = false;
protected DisposableThreadObject()
{
ClassNameCashed = this.GetType().Name;
}
~DisposableThreadObject()
{
Dispose(disposing: false);
}
protected bool Disposed
{
get { return this.GetProperty(ref _disposed, _Locker); }
set { this.SetProperty(ref _disposed, value, _Locker); }
}
protected CancellationTokenSource DisposingCts => DisposingCtsCache ??= new CancellationTokenSource();
protected CancellationTokenSource? DisposingCtsCache
{
get { return this.GetProperty(ref _disposingCtsCached, _Locker); }
private set { this.SetProperty(ref _disposingCtsCached, value, _Locker); }
}
protected CancellationTokenSource LinkTokenSourceDisposing(CancellationToken cancellationToken)
{
return CancellationTokenSource.CreateLinkedTokenSource(DisposingCts.Token, cancellationToken);
}
protected void ThrowIfDisposed()
{
if (Disposed || DisposingCtsCache?.IsCancellationRequested == true)
throw new ObjectDisposedException(ClassNameCashed);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing, bool setDisposed = true)
{
if (!Disposed)
{
if (disposing)
{
// Dispose managed resources here
DisposingCtsCache?.Cancel();
DisposingCtsCache?.Dispose();
}
// Dispose unmanaged resources here
if (setDisposed)
{
Disposed = true;
}
}
}
}
}