static int Main(string[] args) { // setup catch to fatal program crash AppDomain.CurrentDomain.UnhandledException += MyUnhandledException; Application.ThreadException += MyThreadException; // establish file/folder locations Logger.Initialize("Media Center", "EPG123"); EstablishFileFolderPaths(); // create a mutex and keep alive until program exits using (Mutex mutex = new Mutex(true, "Global\\" + appGuid)) { bool showGui = true; bool import = false; bool match = false; bool showProgress = false; epgConfig config = null; // only evaluate arguments if a configuration file exists, otherwise open the gui if (File.Exists(Helper.Epg123CfgPath) && (args != null)) { for (int i = 0; i < args.Length; ++i) { switch (args[i].ToLower()) { case "-update": showGui = false; break; case "-p": showProgress = true; break; default: return(-1); } } } // check for an instance already running if (!mutex.WaitOne(3000, false)) { if (!showGui) { Logger.WriteMessage("==============================================================================="); Logger.WriteError("An instance of EPG123 is already running. Aborting update."); Logger.WriteMessage("==============================================================================="); Logger.Close(); return(-1); } else { MessageBox.Show("An instance of EPG123 is already running.", "Initialization Aborted"); Logger.Close(); return(0); } } // open the configuration GUI if needed if (showGui) { Logger.WriteMessage("==============================================================================="); Logger.WriteMessage(string.Format(" Activating the epg123 configuration GUI. version {0}", Helper.epg123Version)); Logger.WriteMessage("==============================================================================="); frmMain cfgForm = new frmMain(); cfgForm.ShowDialog(); Logger.Close(); if (!cfgForm.Execute) { mutex.ReleaseMutex(); return(0); } Logger.Initialize("Media Center", "EPG123"); config = cfgForm.config; import = cfgForm.import; match = cfgForm.match; } // prevent machine from entering sleep mode uint prevThreadState = NativeMethods.SetThreadExecutionState((uint)ExecutionFlags.ES_CONTINUOUS | (uint)ExecutionFlags.ES_SYSTEM_REQUIRED | (uint)ExecutionFlags.ES_AWAYMODE_REQUIRED); Logger.WriteMessage("==============================================================================="); Logger.WriteMessage(string.Format(" Beginning epg123 update execution. version {0}", Helper.epg123Version)); Logger.WriteMessage("==============================================================================="); // bring in the configuration if (config == null) { try { using (StreamReader stream = new StreamReader(Helper.Epg123CfgPath, Encoding.Default)) { XmlSerializer serializer = new XmlSerializer(typeof(epgConfig)); TextReader reader = new StringReader(stream.ReadToEnd()); config = (epgConfig)serializer.Deserialize(reader); reader.Close(); } } catch (IOException ex) { Logger.WriteError(string.Format("Failed to open configuration file during initialization due to IO exception. message: {0}", ex.Message)); Logger.Close(); NativeMethods.SetThreadExecutionState(prevThreadState | (uint)ExecutionFlags.ES_CONTINUOUS); mutex.ReleaseMutex(); return(-1); } catch (Exception ex) { Logger.WriteError(string.Format("Failed to open configuration file during initialization with unknown exception. message: {0}", ex.Message)); Logger.Close(); NativeMethods.SetThreadExecutionState(prevThreadState | (uint)ExecutionFlags.ES_CONTINUOUS); mutex.ReleaseMutex(); return(-1); } } // let's do this DateTime startTime = DateTime.UtcNow - TimeSpan.FromMinutes(1.0); NotifyIcon notifyIcon = new NotifyIcon() { Text = "EPG123\nDownloading and building guide listings...", Icon = Properties.Resources.EPG123_download }; notifyIcon.Visible = true; if (showGui || showProgress) { frmProgress build = new frmProgress(config); build.ShowDialog(); } else { sdJson2mxf.Build(config); } notifyIcon.Visible = false; notifyIcon.Dispose(); // close the logger and restore power/sleep settings Logger.WriteVerbose(string.Format("epg123 update execution time was {0}.", DateTime.UtcNow - startTime - TimeSpan.FromMinutes(1.0))); Logger.Close(); NativeMethods.SetThreadExecutionState(prevThreadState | (uint)ExecutionFlags.ES_CONTINUOUS); // did a Save&Execute from GUI ... perform import and automatch as well if requested if (sdJson2mxf.success && import) { // verify output file exists if (!File.Exists(Helper.Epg123MxfPath) || !File.Exists(Helper.Epg123ClientExePath)) { mutex.ReleaseMutex(); return(-1); } // epg123client ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "epg123Client.exe", Arguments = "-i \"" + Helper.Epg123MxfPath + "\"" + (match ? " -match" : string.Empty) + (showGui ? " -p" : string.Empty), RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true }; Process proc = Process.Start(startInfo); proc.WaitForExit(); } mutex.ReleaseMutex(); return(0); } }
private static int Main(string[] args) { // setup catch to fatal program crash AppDomain.CurrentDomain.UnhandledException += MyUnhandledException; Application.ThreadException += MyThreadException; // establish file/folder locations Logger.Initialize("Media Center", "EPG123"); Helper.EstablishFileFolderPaths(); bool import, match, showProgress; import = match = showProgress = false; var showGui = true; epgConfig config = null; // only evaluate arguments if a configuration file exists, otherwise open the gui if (File.Exists(Helper.Epg123CfgPath) && args != null) { foreach (var t in args) { switch (t.ToLower()) { case "-update": showGui = false; break; case "-p": showProgress = true; break; default: return(-1); } } } // create a mutex and keep alive until program exits using (var mutex = Helper.GetProgramMutex($"Global\\{AppGuid}", !showGui)) { // check for an instance already running if (mutex == null) { return(-1); } Logger.WriteMessage("==============================================================================="); Logger.WriteMessage($" {(showGui ? "Activating the epg123 configuration GUI." : "Beginning epg123 update execution.")} version {Helper.Epg123Version}"); Logger.WriteMessage("==============================================================================="); Logger.WriteMessage($"*** {Helper.GetOsDescription()} ***"); Logger.WriteMessage($"*** {Helper.GetWmcDescription()} ***"); // open the configuration GUI if needed if (showGui) { var cfgForm = new frmMain(); cfgForm.ShowDialog(); Logger.Close(); if (!cfgForm.Execute) { mutex.ReleaseMutex(); GC.Collect(); if (!cfgForm.RestartAsAdmin) { return(0); } var startInfo = new ProcessStartInfo { FileName = Helper.Epg123ExePath, WorkingDirectory = Helper.ExecutablePath, UseShellExecute = true, Verb = "runas" }; Process.Start(startInfo); return(0); } Logger.Initialize("Media Center", "EPG123"); config = cfgForm.Config; import = config.AutoImport; match = config.Automatch; } // prevent machine from entering sleep mode var prevThreadState = NativeMethods.SetThreadExecutionState((uint)ExecutionFlags.ES_CONTINUOUS | (uint)ExecutionFlags.ES_SYSTEM_REQUIRED | (uint)ExecutionFlags.ES_AWAYMODE_REQUIRED); // bring in the configuration if (config == null) { try { using (var stream = new StreamReader(Helper.Epg123CfgPath, Encoding.Default)) { var serializer = new XmlSerializer(typeof(epgConfig)); TextReader reader = new StringReader(stream.ReadToEnd()); config = (epgConfig)serializer.Deserialize(reader); reader.Close(); } } catch (IOException ex) { Logger.WriteError($"Failed to open configuration file during initialization due to IO exception. message: {ex.Message}"); Logger.Close(); NativeMethods.SetThreadExecutionState(prevThreadState | (uint)ExecutionFlags.ES_CONTINUOUS); mutex.ReleaseMutex(); GC.Collect(); return(-1); } catch (Exception ex) { Logger.WriteError($"Failed to open configuration file during initialization with unknown exception. message: {ex.Message}"); Logger.Close(); NativeMethods.SetThreadExecutionState(prevThreadState | (uint)ExecutionFlags.ES_CONTINUOUS); mutex.ReleaseMutex(); GC.Collect(); return(-1); } } // let's do this Helper.SendPipeMessage("Downloading|Initializing..."); var startTime = DateTime.UtcNow; if (showGui || showProgress) { var build = new frmProgress(config); build.ShowDialog(); } else { sdJson2mxf.sdJson2Mxf.Build(config); if (!sdJson2mxf.sdJson2Mxf.Success) { Logger.WriteError("Failed to create MXF file. Exiting."); } } // close the logger and restore power/sleep settings Logger.WriteVerbose($"epg123 update execution time was {DateTime.UtcNow - startTime}."); Logger.Close(); NativeMethods.SetThreadExecutionState(prevThreadState | (uint)ExecutionFlags.ES_CONTINUOUS); // did a Save&Execute from GUI ... perform import and automatch as well if requested if (!sdJson2mxf.sdJson2Mxf.Success) { return(-1); } if (import) { // verify output file exists if (!File.Exists(Helper.Epg123MxfPath) || !File.Exists(Helper.Epg123ClientExePath)) { mutex.ReleaseMutex(); GC.Collect(); return(-1); } // epg123client var startInfo = new ProcessStartInfo() { FileName = "epg123Client.exe", Arguments = "-i \"" + Helper.Epg123MxfPath + "\" -nogc -noverify -p" + (match ? " -match" : string.Empty), UseShellExecute = false, CreateNoWindow = true }; var proc = Process.Start(startInfo); proc?.WaitForExit(); } return(0); } }