public static bool MessageHandler(ref Message m) { if (m.Msg == WM_ENDSESSION || (m.Msg == WM_QUERYENDSESSION && ShutdownInitiated)) { m.Result = new IntPtr(ENDSESSION_CLOSEAPP); return(true); } else if (m.Msg == WM_QUERYENDSESSION) { //system getting ready to shut down //individual *forms* receive this message, so we must protect against a race condition using (var shutdownMutex = new ScopedMutex(_appName + "{EA433526-9724-43FD-B175-6EA7BA7517A4}")) { if (ShutdownInitiated) { m.Result = new IntPtr(ENDSESSION_CLOSEAPP); return(true); } ShutdownInitiated = true; var recoveryMan = new RecoveryManager(); recoveryMan.CreateRecoveryData(recoveryMan.UnsafeShutdownPath); ShutdownDumpComplete.Set(); m.Result = new IntPtr(ENDSESSION_CLOSEAPP); return(true); } } return(false); }
public void Save() { if (File.Exists(_path)) { File.Delete(_path); } if (!Directory.Exists(Path.GetDirectoryName(_path))) { Directory.CreateDirectory(Path.GetDirectoryName(_path)); } var serializer = new DataContractJsonSerializer(typeof(Preferences)); using (var mutex = new ScopedMutex("{F995FD2B-04EC-4410-AB57-9B4E6FF4B2B2}")) using (var fstream = new FileStream(_path, FileMode.Create)) { mutex.WaitOne(); serializer.WriteObject(fstream, this); } }