| | | 1 | | using System; |
| | | 2 | | using System.Threading; |
| | | 3 | | using System.Threading.Tasks; |
| | | 4 | | |
| | | 5 | | namespace Renci.SshNet.Abstractions |
| | | 6 | | { |
| | | 7 | | internal static class ThreadAbstraction |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Suspends the current thread for the specified number of milliseconds. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <param name="millisecondsTimeout">The number of milliseconds for which the thread is suspended.</param> |
| | | 13 | | public static void Sleep(int millisecondsTimeout) |
| | 0 | 14 | | { |
| | 0 | 15 | | Thread.Sleep(millisecondsTimeout); |
| | 0 | 16 | | } |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Creates and starts a long-running <see cref="Task"/> for the specified <see cref="Action"/>. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="action">The <see cref="Action"/> to start.</param> |
| | | 22 | | /// <exception cref="ArgumentNullException"><paramref name="action"/> is <see langword="null"/>.</exception> |
| | | 23 | | /// <returns> |
| | | 24 | | /// A task that represents the execution of the specified <see cref="Action"/>. |
| | | 25 | | /// </returns> |
| | | 26 | | public static Task ExecuteThreadLongRunning(Action action) |
| | 1817 | 27 | | { |
| | 1817 | 28 | | if (action is null) |
| | 0 | 29 | | { |
| | 0 | 30 | | throw new ArgumentNullException(nameof(action)); |
| | | 31 | | } |
| | | 32 | | |
| | 1817 | 33 | | return Task.Factory.StartNew(action, |
| | 1817 | 34 | | CancellationToken.None, |
| | 1817 | 35 | | TaskCreationOptions.LongRunning, |
| | 1817 | 36 | | TaskScheduler.Current); |
| | 1817 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Executes the specified action in a separate thread. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="action">The action to execute.</param> |
| | | 43 | | public static void ExecuteThread(Action action) |
| | 3672 | 44 | | { |
| | 3672 | 45 | | if (action is null) |
| | 0 | 46 | | { |
| | 0 | 47 | | throw new ArgumentNullException(nameof(action)); |
| | | 48 | | } |
| | | 49 | | |
| | 7344 | 50 | | _ = ThreadPool.QueueUserWorkItem(o => action()); |
| | 3672 | 51 | | } |
| | | 52 | | } |
| | | 53 | | } |