PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/base/Applications/Sleep/Sleep.cs

#
C# | 91 lines | 66 code | 16 blank | 9 comment | 4 complexity | 8be81845b99ddad980d1640cf8a9b139 MD5 | raw file
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Research Singularity
  4. //
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. // Note: Shell utility to pause execution.
  8. //
  9. using System;
  10. using System.Threading;
  11. // using Microsoft.Singularity.V1.Services;
  12. using Microsoft.Contracts;
  13. using Microsoft.SingSharp.Reflection;
  14. using Microsoft.Singularity.Applications;
  15. using Microsoft.Singularity.Channels;
  16. using Microsoft.Singularity.Io;
  17. using Microsoft.Singularity.Configuration;
  18. [assembly: Transform(typeof(ApplicationResourceTransform))]
  19. namespace Microsoft.Singularity.Applications
  20. {
  21. [ConsoleCategory(DefaultAction=true)]
  22. internal class Parameters {
  23. [InputEndpoint("data")]
  24. public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
  25. [OutputEndpoint("data")]
  26. public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
  27. [LongParameter( "seconds", Mandatory=true, Position=0, HelpMessage="seconds")]
  28. internal long seconds;
  29. [BoolParameter( "m", Default=false , HelpMessage="treat time as milliseconds")]
  30. internal bool doMilli;
  31. [BoolParameter( "v", Default=false , HelpMessage="verbose output")]
  32. internal bool doVerbose;
  33. reflective internal Parameters();
  34. internal int AppMain() {
  35. return Sleep.AppMain(this);
  36. }
  37. }
  38. public class Sleep
  39. {
  40. private static void ConsoleWriteDateTime(string preamble, DateTime dt)
  41. {
  42. Console.WriteLine("{0}{1:d4}/{2:d2}/{3:d2} {4:d2}:{5:d2}:{6:d2}.{7:d3}",
  43. preamble, dt.Year, dt.Month, dt.Day,
  44. dt.Hour, dt.Minute, dt.Second, dt.Millisecond);
  45. }
  46. private static void ConsoleWriteDateTime(string preamble, TimeSpan sp)
  47. {
  48. Console.WriteLine("{0}{1}", preamble, sp.ToString());
  49. }
  50. private static void DoDate(string pre)
  51. {
  52. Console.WriteLine(pre + "cycles: {0}", Processor.CycleCount );
  53. ConsoleWriteDateTime(pre , DateTime.UtcNow);
  54. }
  55. internal static int AppMain(Parameters! config)
  56. {
  57. try {
  58. uint seconds = (uint) config.seconds;
  59. if (config.doVerbose) DoDate(" pre ");
  60. if (config.doMilli) {
  61. Thread.Sleep(TimeSpan.FromMilliseconds(seconds));
  62. }
  63. else {
  64. Thread.Sleep(TimeSpan.FromSeconds(seconds));
  65. }
  66. if (config.doVerbose) DoDate(" post ");
  67. return 0;
  68. }
  69. catch (ArgumentOutOfRangeException) { // From Thread.Sleep
  70. Console.WriteLine("Value too large :- \"{0}\"", config.seconds);
  71. }
  72. return 1;
  73. }
  74. }
  75. }