MainStartup.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using MediaBrowser.Common.Constants;
  2. using MediaBrowser.Common.Implementations.Updates;
  3. using MediaBrowser.Server.Implementations;
  4. using System;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Threading;
  8. using System.Windows;
  9. using Microsoft.Win32;
  10. namespace MediaBrowser.ServerApplication
  11. {
  12. public class MainStartup
  13. {
  14. /// <summary>
  15. /// The single instance mutex
  16. /// </summary>
  17. private static Mutex _singleInstanceMutex;
  18. private static IApplicationInterface _applicationInterface;
  19. /// <summary>
  20. /// Defines the entry point of the application.
  21. /// </summary>
  22. [STAThread]
  23. public static void Main()
  24. {
  25. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  26. bool createdNew;
  27. var runningPath = Process.GetCurrentProcess().MainModule.FileName.Replace(Path.DirectorySeparatorChar.ToString(), string.Empty);
  28. _singleInstanceMutex = new Mutex(true, @"Local\" + runningPath, out createdNew);
  29. if (!createdNew)
  30. {
  31. _singleInstanceMutex = null;
  32. return;
  33. }
  34. // Look for the existence of an update archive
  35. var appPaths = new ServerApplicationPaths();
  36. var updateArchive = Path.Combine(appPaths.TempUpdatePath, Constants.MbServerPkgName + ".zip");
  37. if (File.Exists(updateArchive))
  38. {
  39. // Update is there - execute update
  40. try
  41. {
  42. new ApplicationUpdater().UpdateApplication(MBApplication.MBServer, appPaths, updateArchive);
  43. // And just let the app exit so it can update
  44. return;
  45. }
  46. catch (Exception e)
  47. {
  48. MessageBox.Show(string.Format("Error attempting to update application.\n\n{0}\n\n{1}", e.GetType().Name, e.Message));
  49. }
  50. }
  51. StartApplication();
  52. }
  53. private static void StartApplication()
  54. {
  55. SystemEvents.SessionEnding += SystemEvents_SessionEnding;
  56. var commandLineArgs = Environment.GetCommandLineArgs();
  57. if (commandLineArgs.Length > 1 && commandLineArgs[1].Equals("-service"))
  58. {
  59. // Start application as a service
  60. StartBackgroundService();
  61. }
  62. else
  63. {
  64. StartWpfApp();
  65. }
  66. }
  67. static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
  68. {
  69. // Try to shutdown gracefully
  70. if (_applicationInterface != null)
  71. {
  72. _applicationInterface.ShutdownApplication();
  73. }
  74. }
  75. private static void StartWpfApp()
  76. {
  77. var app = new App();
  78. _applicationInterface = app;
  79. app.Run();
  80. }
  81. private static void StartBackgroundService()
  82. {
  83. }
  84. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  85. {
  86. var exception = (Exception)e.ExceptionObject;
  87. if (_applicationInterface != null)
  88. {
  89. _applicationInterface.OnUnhandledException(exception);
  90. }
  91. if (!Debugger.IsAttached)
  92. {
  93. Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
  94. }
  95. }
  96. /// <summary>
  97. /// Releases the mutex.
  98. /// </summary>
  99. internal static void ReleaseMutex()
  100. {
  101. if (_singleInstanceMutex == null)
  102. {
  103. return;
  104. }
  105. _singleInstanceMutex.ReleaseMutex();
  106. _singleInstanceMutex.Close();
  107. _singleInstanceMutex.Dispose();
  108. _singleInstanceMutex = null;
  109. }
  110. }
  111. }