-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServiceExecutionLifetime.cs
More file actions
119 lines (108 loc) · 3.88 KB
/
ServiceExecutionLifetime.cs
File metadata and controls
119 lines (108 loc) · 3.88 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using RapidField.SolidInstruments.Core;
using System;
using System.Diagnostics;
using System.Threading;
namespace RapidField.SolidInstruments.Service
{
/// <summary>
/// Provides control over the lifetime of execution for a service.
/// </summary>
/// <remarks>
/// <see cref="ServiceExecutionLifetime" /> is the default implementation of <see cref="IServiceExecutionLifetime" />.
/// </remarks>
public sealed class ServiceExecutionLifetime : Instrument, IServiceExecutionLifetime
{
/// <summary>
/// Initializes a new instance of the <see cref="ServiceExecutionLifetime" /> class.
/// </summary>
[DebuggerHidden]
internal ServiceExecutionLifetime()
: base()
{
IsAlive = true;
}
/// <summary>
/// Unblocks waiting threads and ends the execution lifetime.
/// </summary>
/// <exception cref="ObjectDisposedException">
/// The object is disposed.
/// </exception>
public void End()
{
using (var controlToken = StateControl.Enter())
{
RejectIfDisposed();
if (IsAlive)
{
EndOfLifeEvent.Set();
IsAlive = false;
}
}
}
/// <summary>
/// Blocks the current thread until <see cref="End" /> or <see cref="Dispose" /> is invoked.
/// </summary>
/// <exception cref="InvalidOperationException">
/// The service execution lifetime has ended.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// The object is disposed.
/// </exception>
public void KeepAlive()
{
using (var controlToken = StateControl.Enter())
{
RejectIfDisposed();
if (IsAlive == false)
{
throw new InvalidOperationException("The service execution lifetime has ended.");
}
}
EndOfLifeEvent.WaitOne();
}
/// <summary>
/// Releases all resources consumed by the current <see cref="ServiceExecutionLifetime" />.
/// </summary>
/// <param name="disposing">
/// A value indicating whether or not managed resources should be released.
/// </param>
protected override void Dispose(Boolean disposing)
{
try
{
if (disposing)
{
using (var controlToken = StateControl.Enter())
{
if (IsAlive)
{
EndOfLifeEvent.Set();
IsAlive = false;
}
EndOfLifeEvent.Dispose();
}
}
}
finally
{
base.Dispose(disposing);
}
}
/// <summary>
/// Gets a value that indicates whether or not the service is operational.
/// </summary>
public Boolean IsAlive
{
get;
private set;
}
/// <summary>
/// Represents an event that can be triggered to signal the end of an associated service's lifetime.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly ManualResetEvent EndOfLifeEvent = new ManualResetEvent(false);
}
}