private static void Main() { try { LogInformation("Program started"); if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1) { const int flags = (int)NativeMethods.MessageBroadcastFlags.BSF_POSTMESSAGE; var recipients = (int)NativeMethods.MessageBroadcastRecipients.BSM_APPLICATIONS; NativeMethods.BroadcastSystemMessage(flags, ref recipients, NativeMethods.WM_NOTIFYDD, 0, 0); LogInformation("Second instance detected"); return; } UpgradeSettings(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); SystemEvents.SessionEnding += SystemEventsSessionEnding; _deskDriveForm = new DeskDrive(); Application.Run(_deskDriveForm); Desktop.SaveIconPositions(); } catch (Exception ex) { LogError("Unhandled exception: " + ex); throw; } finally { LogInformation("Program terminated"); } }
private static void SystemEventsSessionEnding(object sender, SessionEndingEventArgs e) { SystemEvents.SessionEnding -= SystemEventsSessionEnding; Desktop.SaveIconPositions(); }