static void SetRegistryDataConfiguration(RegistryDataConfiguration registryDataConfiguration) { lock (locker) { //EventWaitHandle eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, ""); //eventWaitHandle. //using (var mutex = new Mutex(false, "RegistryDataConfiguration")) //{ // mutex.WaitOne(TimeSpan.FromSeconds(1)); for (int i = 0; i < 3; i++) { var tempFileName = FileName + "." + Guid.NewGuid().ToString(); try { var xmlSerializer = new XmlSerializer(typeof(RegistryDataConfiguration)); using (var fileStream = new FileStream(tempFileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) { xmlSerializer.Serialize(fileStream, registryDataConfiguration); } File.Copy(tempFileName, FileName, true); return; } catch (Exception e) { Logger.Error(e, "RegistrySettingsHelper.SetRegistryDataConfiguration"); } finally { if (File.Exists(tempFileName)) { File.Delete(tempFileName); } //mutex.ReleaseMutex(); } } //} } }
static RegistryDataConfiguration GetRegistryDataConfiguration() { lock (locker) { //using (var mutex = new Mutex(true, "RegistryDataConfiguration")) //{ // mutex.WaitOne(TimeSpan.FromSeconds(1)); for (int i = 0; i < 3; i++) { var tempFileName = FileName + "." + Guid.NewGuid().ToString(); try { var registryDataConfiguration = new RegistryDataConfiguration(); if (File.Exists(FileName)) { File.Copy(FileName, tempFileName); using (var fileStream = new FileStream(tempFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var xmlSerializer = new XmlSerializer(typeof(RegistryDataConfiguration)); registryDataConfiguration = (RegistryDataConfiguration)xmlSerializer.Deserialize(fileStream); } } return registryDataConfiguration; } catch (Exception e) { Logger.Error(e, "RegistrySettingsHelper.GetRegistryDataConfiguration "); } finally { if (File.Exists(tempFileName)) { File.Delete(tempFileName); } //mutex.ReleaseMutex(); } } return new RegistryDataConfiguration(); //} } }