forked from chocolatey/choco
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a Console.ReadLine with a timeout so that when confirmation is selected it doesn't block the thread eternally. This allows folks to put prompts in chocolatey automation scripts with a default selection and other fun prompting actions.
- Loading branch information
1 parent
22b8171
commit 1076287
Showing
4 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/chocolatey/infrastructure/commandline/ReadLineTimeout.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright © 2011 - Present RealDimensions Software, LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
namespace chocolatey.infrastructure.commandline | ||
{ | ||
using System; | ||
using System.Threading; | ||
|
||
/// <summary> | ||
/// Because sometimes you to timeout a readline instead of blocking infinitely. | ||
/// </summary> | ||
/// <remarks> | ||
/// Based on http://stackoverflow.com/a/18342182/18475 | ||
/// </remarks> | ||
public class ReadLineTimeout : IDisposable | ||
{ | ||
private readonly AutoResetEvent _backgroundResponseReset; | ||
private readonly AutoResetEvent _foregroundResponseReset; | ||
private string _input; | ||
private readonly Thread _responseThread; | ||
|
||
private bool _isDisposing; | ||
|
||
private ReadLineTimeout() | ||
{ | ||
_backgroundResponseReset = new AutoResetEvent(false); | ||
_foregroundResponseReset = new AutoResetEvent(false); | ||
_responseThread = new Thread(console_read) | ||
{ | ||
IsBackground = true | ||
}; | ||
_responseThread.Start(); | ||
} | ||
|
||
private void console_read() | ||
{ | ||
while (true) | ||
{ | ||
_backgroundResponseReset.WaitOne(); | ||
_input = Console.ReadLine(); | ||
_foregroundResponseReset.Set(); | ||
} | ||
} | ||
|
||
public static string read(int timeoutMilliseconds) | ||
{ | ||
using (var readLine = new ReadLineTimeout()) | ||
{ | ||
readLine._backgroundResponseReset.Set(); | ||
|
||
return readLine._foregroundResponseReset.WaitOne(timeoutMilliseconds) ? | ||
readLine._input | ||
: null; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_isDisposing) return; | ||
|
||
_isDisposing = true; | ||
_responseThread.Abort(); | ||
_backgroundResponseReset.Close(); | ||
_backgroundResponseReset.Dispose(); | ||
_foregroundResponseReset.Close(); | ||
_foregroundResponseReset.Dispose(); | ||
} | ||
} | ||
} |