VirtualRoot.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading;
  7. namespace NTMiner {
  8. public static partial class VirtualRoot {
  9. private static string s_sha1 = null;
  10. public static string Sha1 {
  11. get {
  12. if (s_sha1 == null) {
  13. s_sha1 = HashUtil.Sha1(File.ReadAllBytes(Process.GetCurrentProcess().MainModule.FileName));
  14. }
  15. return s_sha1;
  16. }
  17. }
  18. public static DateTime StartedOn { get; private set; } = DateTime.Now;
  19. public static EventWaitHandle _waitHandle;
  20. private static Mutex _sMutexApp;
  21. // 注意:该程序编译成无界面的windows应用程序而不是控制台程序,从而随机自动启动时无界面
  22. [STAThread]
  23. static void Main(string[] args) {
  24. HomePath.SetHomeDirFullName(AppDomain.CurrentDomain.BaseDirectory);
  25. if (args.Length != 0) {
  26. if (args.Contains("--sha1", StringComparer.OrdinalIgnoreCase)) {
  27. File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sha1"), Sha1);
  28. return;
  29. }
  30. }
  31. try {
  32. if (DevMode.IsDevMode) {
  33. NTMinerConsole.GetOrAlloc();
  34. }
  35. SystemEvents.SessionEnding += SessionEndingEventHandler;
  36. StartTimer();
  37. _waitHandle = new AutoResetEvent(false);
  38. bool mutexCreated;
  39. try {
  40. _sMutexApp = new Mutex(true, "NTMinerNoDevFeeAppMutex", out mutexCreated);
  41. }
  42. catch {
  43. mutexCreated = false;
  44. }
  45. if (mutexCreated) {
  46. if (!DevMode.IsDevMode) {
  47. Write.Disable();
  48. }
  49. NTMinerRegistry.SetNoDevFeeVersion(Sha1);
  50. NTMinerRegistry.SetAutoBoot("NTMinerNoDevFee", true);
  51. Run();
  52. }
  53. }
  54. catch (Exception e) {
  55. Logger.ErrorDebugLine(e);
  56. }
  57. }
  58. private static void Run() {
  59. try {
  60. Windows.ConsoleHandler.Register(Exit);
  61. AddEventPath<Per10SecondEvent>("呼吸表示活着", LogEnum.None,
  62. action: message => {
  63. NTMinerRegistry.SetNoDevFeeActiveOn(DateTime.Now);
  64. NoDevFee.NoDevFeeUtil.StartAsync();
  65. }, typeof(VirtualRoot));
  66. _waitHandle.WaitOne();
  67. Exit();
  68. }
  69. catch (Exception e) {
  70. Logger.ErrorDebugLine(e);
  71. }
  72. finally {
  73. Exit();
  74. }
  75. }
  76. private static bool _isClosed = false;
  77. public static void Exit() {
  78. if (!_isClosed) {
  79. _isClosed = true;
  80. RaiseEvent(new AppExitEvent());
  81. _sMutexApp?.Dispose();
  82. NTMinerConsole.Free();
  83. Environment.Exit(0);
  84. }
  85. }
  86. }
  87. }