public void OnSrpRecordAdded(object source, FileSystemEventArgs e) { OnMatchEvent?.Invoke(); const int CONST_ATTEMPTS = 16; // read last line String last = ""; bool isLocked = true; int i = 0; while (isLocked && i < CONST_ATTEMPTS) { try { using (FileStream srpFileStream = File.Open(AppData.SrpFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { srpFileStream.Position = LastPosition; LastPosition = srpFileStream.Length; using (StreamReader srpStreamReader = new StreamReader(srpFileStream, Encoding.Unicode)) { last = srpStreamReader.ReadToEnd(); if (last.EndsWith(Environment.NewLine)) { last = last.Remove(last.Length - AppData.NewLineLength, AppData.NewLineLength); } foreach (string line in last.Split(Environment.NewLine.ToCharArray())) { if (!String.IsNullOrEmpty(line)) { AppData._nLog.Debug(line); } } } break; } } catch (IOException ex) { int errorCode = Marshal.GetHRForException(ex) & ((1 << 16) - 1); isLocked = errorCode == 32 || errorCode == 33; i++; if (!isLocked) { throw new Exception(ex.Message); } } } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnRegChanged(object sender, EventArgs e) { LoadDataFromRegistry(false); OnMatchEvent?.Invoke(); }