forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartSleepCommand.cs
More file actions
141 lines (117 loc) · 4.1 KB
/
Copy pathStartSleepCommand.cs
File metadata and controls
141 lines (117 loc) · 4.1 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Management.Automation;
using System.Threading;
using Dbg = System.Management.Automation;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Suspend shell, script, or runspace activity for the specified period of time.
/// </summary>
[Cmdlet(VerbsLifecycle.Start, "Sleep", DefaultParameterSetName = "Seconds", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113407")]
public sealed class StartSleepCommand : PSCmdlet, IDisposable
{
private bool _disposed = false;
#region IDisposable
/// <summary>
/// Dispose method of IDisposable interface.
/// </summary>
public void Dispose()
{
if (_disposed == false)
{
if (_waitHandle != null)
{
_waitHandle.Dispose();
_waitHandle = null;
}
_disposed = true;
}
}
#endregion
#region parameters
/// <summary>
/// Allows sleep time to be specified in seconds
/// </summary>
[Parameter(Position = 0, Mandatory = true, ParameterSetName = "Seconds", ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]
[ValidateRangeAttribute(0, int.MaxValue / 1000)]
public int Seconds { get; set; }
/// <summary>
/// Allows sleep time to be specified in milliseconds
/// </summary>
[Parameter(Mandatory = true, ParameterSetName = "Milliseconds", ValueFromPipelineByPropertyName = true)]
[ValidateRangeAttribute(0, int.MaxValue)]
public int Milliseconds { get; set; }
#endregion
#region methods
//Wait handle which is used by thread to sleep.
private ManualResetEvent _waitHandle;
//object used for synchronizes pipeline thread and stop thread
//access to waitHandle
private object _syncObject = new object();
//this is set to true by stopProcessing
private bool _stopping = false;
/// <summary>
/// This method causes calling thread to sleep for
/// specified milliseconds
/// </summary>
private void Sleep(int milliSecondsToSleep)
{
lock (_syncObject)
{
if (_stopping == false)
{
_waitHandle = new ManualResetEvent(false);
}
}
if (_waitHandle != null)
{
#if CORECLR //TODO:CORECLR bool WaitOne(int millisecondsTimeout,bool exitContext) is not available on CLR yet
_waitHandle.WaitOne(new TimeSpan(0, 0, 0, 0, milliSecondsToSleep));
#else
_waitHandle.WaitOne(new TimeSpan(0, 0, 0, 0, milliSecondsToSleep), true);
#endif
}
}
/// <summary>
///
/// </summary>
protected override void ProcessRecord()
{
int sleepTime = 0;
switch (ParameterSetName)
{
case "Seconds":
sleepTime = Seconds * 1000;
break;
case "Milliseconds":
sleepTime = Milliseconds;
break;
default:
Dbg.Diagnostics.Assert(false, "Only one of the specified parameter sets should be called.");
break;
}
Sleep(sleepTime);
} // EndProcessing
/// <summary>
/// stopprocessing override
/// </summary>
protected override
void
StopProcessing()
{
lock (_syncObject)
{
_stopping = true;
if (_waitHandle != null)
{
_waitHandle.Set();
}
}
}
#endregion
} // StartSleepCommand
} // namespace Microsoft.PowerShell.Commands