ThreadAbstraction.cs 1.9 KB

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