using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Reflection; using System.Runtime.InteropServices; using System.Resources; using System.Threading; using System.Diagnostics; namespace Project { static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); if (CheckSingleInstance() == false) return; PUB.initCore(); Application.Run(new fMain()); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { string emsg = "Fatal Error(UHE)\n\n" + e.ExceptionObject.ToString(); PUB.log_[0].AddE(emsg); PUB.log_[0].Flush(); PUB.log_[1].AddE(emsg); PUB.log_[1].Flush(); Util.SaveBugReport(emsg); Shutdown(); var f = new fErrorException(emsg); f.ShowDialog(); Application.ExitThread(); } /// /// Check for duplicate execution prevention /// /// true if single instance, false if duplicate execution static bool CheckSingleInstance() { string processName = Process.GetCurrentProcess().ProcessName; Process[] processes = Process.GetProcessesByName(processName); if (processes.Length > 1) { // 중복실행 감지 string message = $"⚠️ {Application.ProductName} program is already running!\n\n" + "Cannot run multiple programs simultaneously.\n\n" + "Please select a solution:"; var result = MessageBox.Show(message + "\n\nYes(Y): Terminate existing program and start new\nNo(N): Cancel current execution", "Duplicate Execution Detected", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) { // 기존 프로세스들을 종료 try { int currentProcessId = Process.GetCurrentProcess().Id; foreach (Process process in processes) { if (process.Id != currentProcessId) { process.Kill(); process.WaitForExit(3000); // 3초 대기 } } // 잠시 대기 후 계속 진행 Thread.Sleep(1000); return true; } catch (Exception ex) { MessageBox.Show($"An error occurred while terminating the existing program:\n{ex.Message}\n\n" + "Please terminate manually in Task Manager.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } else { // 현재 실행을 취소 return false; } } return true; // 단일 인스턴스 } static void Shutdown() { PUB.log_[0].Add("Program Close"); PUB.log_[0].Flush(); PUB.log_[1].Add("Program Close"); PUB.log_[1].Flush(); } static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { string emsg = "Fatal Error(ATE)\n\n" + e.Exception.ToString(); PUB.log_[0].AddE(emsg); PUB.log_[0].Flush(); PUB.log_[1].AddE(emsg); PUB.log_[1].Flush(); AR.UTIL.SaveBugReport(emsg); Shutdown(); var f = new fErrorException(emsg); f.ShowDialog(); Application.ExitThread(); } } }