Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ConsoleIn.cs
1using System;
2using System.Threading.Tasks;
4
6{
11 public static class ConsoleIn
12 {
20 public static int Peek()
21 {
22 return PeekAsync().Result;
23 }
24
32 public static async Task<int> PeekAsync()
33 {
34 TaskCompletionSource<int> Result = new TaskCompletionSource<int>();
35 if (await ConsoleWorker.Queue(new ConsoleInPeekCharacter(Result)))
36 return await Result.Task;
37 else
38 return -1;
39 }
40
46 public static int Read()
47 {
48 return ReadAsync().Result;
49 }
50
56 public static async Task<int> ReadAsync()
57 {
58 TaskCompletionSource<int> Result = new TaskCompletionSource<int>();
59 if (await ConsoleWorker.Queue(new ConsoleInReadCharacter(Result)))
60 return await Result.Task;
61 else
62 return -1;
63 }
64
80 public static int Read(char[] buffer, int index, int count)
81 {
82 return ReadAsync(buffer, index, count).Result;
83 }
84
101 public static async Task<int> ReadAsync(char[] buffer, int index, int count)
102 {
103 CheckArguments(buffer, index, count);
104
105 TaskCompletionSource<int> Result = new TaskCompletionSource<int>();
106 if (await ConsoleWorker.Queue(new ConsoleInRead(buffer, index, count, Result)))
107 return await Result.Task;
108 else
109 return 0;
110 }
111
112 private static void CheckArguments(Array buffer, int index, int count)
113 {
114 if (buffer is null)
115 throw new ArgumentNullException(nameof(buffer));
116
117 if (index < 0)
118 throw new ArgumentOutOfRangeException(nameof(index));
119
120 if (count < 0)
121 throw new ArgumentOutOfRangeException(nameof(count));
122
123 if (index > buffer.Length)
124 throw new ArgumentException(nameof(index));
125
126 if (index + count > buffer.Length)
127 throw new ArgumentException(nameof(count));
128 }
129
141 public static int ReadBlock(char[] buffer, int index, int count)
142 {
143 return ReadBlockAsync(buffer, index, count).Result;
144 }
145
164 public static async Task<int> ReadBlockAsync(char[] buffer, int index, int count)
165 {
166 CheckArguments(buffer, index, count);
167
168 TaskCompletionSource<int> Result = new TaskCompletionSource<int>();
169 if (await ConsoleWorker.Queue(new ConsoleInReadBlock(buffer, index, count, Result)))
170 return await Result.Task;
171 else
172 return 0;
173 }
174
179 public static string ReadLine()
180 {
181 return ReadLineAsync().Result;
182 }
183
190 public static async Task<string> ReadLineAsync()
191 {
192 TaskCompletionSource<string> Result = new TaskCompletionSource<string>();
193 if (await ConsoleWorker.Queue(new ConsoleInReadLine(Result)))
194 return await Result.Task;
195 else
196 return null;
197 }
198
205 public static string ReadToEnd()
206 {
207 return ReadToEndAsync().Result;
208 }
209
217 public static async Task<string> ReadToEndAsync()
218 {
219 TaskCompletionSource<string> Result = new TaskCompletionSource<string>();
220 if (await ConsoleWorker.Queue(new ConsoleInReadToEnd(Result)))
221 return await Result.Task;
222 else
223 return string.Empty;
224 }
225
230 public static ConsoleKeyInfo ReadKey()
231 {
232 return ReadKeyAsync().Result;
233 }
234
239 public static ConsoleKeyInfo ReadKey(bool intercept)
240 {
241 return ReadKeyAsync(intercept).Result;
242 }
243
248 public static Task<ConsoleKeyInfo> ReadKeyAsync()
249 {
250 return ReadKeyAsync(false);
251 }
252
257 public static async Task<ConsoleKeyInfo> ReadKeyAsync(bool intercept)
258 {
259 TaskCompletionSource<ConsoleKeyInfo> Result = new TaskCompletionSource<ConsoleKeyInfo>();
260 if (await ConsoleWorker.Queue(new ConsoleInReadKey(intercept, Result)))
261 return await Result.Task;
262 else
263 return new ConsoleKeyInfo();
264 }
265
266 }
267}
Serializes input from System.Console.In, and assures modules are not dead-locked in case the Console ...
Definition: ConsoleIn.cs:12
static ConsoleKeyInfo ReadKey()
Reads a key press
Definition: ConsoleIn.cs:230
static string ReadLine()
Reads a line of characters from the text reader and returns the data as a string.
Definition: ConsoleIn.cs:179
static async Task< int > ReadAsync(char[] buffer, int index, int count)
Reads a specified maximum number of characters from the current text reader asynchronously and writes...
Definition: ConsoleIn.cs:101
static async Task< string > ReadLineAsync()
Reads a line of characters asynchronously and returns the data as a string.
Definition: ConsoleIn.cs:190
static Task< ConsoleKeyInfo > ReadKeyAsync()
Reads a key press
Definition: ConsoleIn.cs:248
static async Task< ConsoleKeyInfo > ReadKeyAsync(bool intercept)
Reads a key press
Definition: ConsoleIn.cs:257
static string ReadToEnd()
Reads all characters from the current position to the end of the text reader and returns them as one ...
Definition: ConsoleIn.cs:205
static async Task< int > PeekAsync()
Reads the next character without changing the state of the reader or the character source....
Definition: ConsoleIn.cs:32
static int Peek()
Reads the next character without changing the state of the reader or the character source....
Definition: ConsoleIn.cs:20
static async Task< int > ReadBlockAsync(char[] buffer, int index, int count)
Reads a specified maximum number of characters from the current text reader asynchronously and writes...
Definition: ConsoleIn.cs:164
static async Task< string > ReadToEndAsync()
Reads all characters from the current position to the end of the text reader asynchronously and retur...
Definition: ConsoleIn.cs:217
static int Read()
Reads the next character from the text reader and advances the character position by one character.
Definition: ConsoleIn.cs:46
static int ReadBlock(char[] buffer, int index, int count)
Reads a specified maximum number of characters from the current text reader and writes the data to a ...
Definition: ConsoleIn.cs:141
static ConsoleKeyInfo ReadKey(bool intercept)
Reads a key press
Definition: ConsoleIn.cs:239
static async Task< int > ReadAsync()
Reads the next character from the text reader and advances the character position by one character.
Definition: ConsoleIn.cs:56
static int Read(char[] buffer, int index, int count)
Reads a specified maximum number of characters from the current reader and writes the data to a buffe...
Definition: ConsoleIn.cs:80
Reads a block of characters from the console.
Reads characters from the console.
Processes console tasks, such as input and output, in a serialized asynchronous manner.
static Task< bool > Queue(WorkItem Work)
Queues a work item.