public CompareTo ( object obj ) : int | ||
obj | object | The object being compared. |
return | int |
private void ProcessSyncFile() { // Record if the sync file exists to avoid repeated calls // to the operating system. if (_syncFileExists == false) { _syncFileExists = File.Exists(_syncFilePath); } if (_syncFileExists) { // Used to indicate if the process should be repeated. bool repeatProcess = false; // Lock the list of devices we're about to update to ensure they can't be // changed by subsequent requests to this callback. lock (_previous.Requests) { // Open the sync file for read access ensuring it's disposed // as soon as possible. FileStream stream = OpenSyncFilePath(FileMode.Open, FileAccess.Read, FileShare.ReadWrite); if (stream != null) { // Record the length of the file so that if it changes we can abandon this // update and rely on a subsequent call to this methd to complete // processing. using (BinaryReader reader = new BinaryReader(stream)) { long length = stream.Length; RequestRecord record = new RequestRecord(); RequestRecord firstDevice = null; long rows = length / RECORD_LENGTH; for (long row = 0; row < rows; row++) { // Read the current row in reverse order. Capture EOF exceptions // in case the file length has changed since we started processing. try { stream.Position = ((rows - row) * RECORD_LENGTH) - RECORD_LENGTH; record.Read(reader); } catch (EndOfStreamException ex) { // The file has been trimmed by another process. Break and // allow the resulting call to this event to complete // processing. EventLog.Debug(ex); break; } // If the current record is the same as the last one we got last time // this method was called then stop processing more records. if (record.CompareTo(_previous.lastRequest) == 0) { break; } // Update the memory version. if (record.LastActiveDate == 0) { // Remove from the device as the last active date is zero. _previous.Requests.Remove(record.Key); } else { // Update or insert a new record. if (_previous.Requests.ContainsKey(record.Key)) { _previous.Requests[record.Key] = record.LastActiveDate; } else { _previous.Requests.Add(record.Key, record.LastActiveDate); } } if (firstDevice == null) { firstDevice = new RequestRecord(record); } } // If the length of the file hasn't changed during the processing // then update the last device record to limit the number of rows // examined in future file changes. if (length == stream.Length && firstDevice != null) { _previous.lastRequest = firstDevice; } // Signal to all the method again if the length of the file // has changed during processing. repeatProcess = length != stream.Length; } } } // If the file was altered during the processing then call the method // again to capture any new records. if (repeatProcess) { ProcessSyncFile(); } } }
private void ProcessSyncFile() { // Record if the sync file exists to avoid repeated calls // to the operating system. if (_syncFileExists == false) _syncFileExists = File.Exists(_syncFilePath); if (_syncFileExists) { // Used to indicate if the process should be repeated. bool repeatProcess = false; // Lock the list of devices we're about to update to ensure they can't be // changed by subsequent requests to this callback. lock (_previous.Requests) { // Open the sync file for read access ensuring it's disposed // as soon as possible. using (FileStream stream = OpenSyncFilePath(FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { if (stream != null) { // Record the length of the file so that if it changes we can abandon this // update and rely on a subsequent call to this methd to complete // processing. long length = stream.Length; BinaryReader reader = new BinaryReader(stream); RequestRecord record = new RequestRecord(); RequestRecord firstDevice = null; long rows = length/RECORD_LENGTH; for (long row = 0; row < rows; row++) { // Read the current row in reverse order. Capture EOF exceptions // in case the file length has changed since we started processing. try { stream.Position = ((rows - row)*RECORD_LENGTH) - RECORD_LENGTH; record.Read(reader); } catch (EndOfStreamException ex) { // The file has been trimmed by another process. Break and // allow the resulting call to this event to complete // processing. EventLog.Debug(ex); break; } // If the current record is the same as the last one we got last time // this method was called then stop processing more records. if (record.CompareTo(_previous.lastRequest) == 0) break; // Update the memory version. if (record.LastActiveDate == 0) { // Remove from the device as the last active date is zero. _previous.Requests.Remove(record.Key); } else { // Update or insert a new record. if (_previous.Requests.ContainsKey(record.Key)) _previous.Requests[record.Key] = record.LastActiveDate; else _previous.Requests.Add(record.Key, record.LastActiveDate); } if (firstDevice == null) firstDevice = new RequestRecord(record); } // If the length of the file hasn't changed during the processing // then update the last device record to limit the number of rows // examined in future file changes. if (length == stream.Length && firstDevice != null) _previous.lastRequest = firstDevice; // Signal to all the method again if the length of the file // has changed during processing. repeatProcess = length != stream.Length; reader.Close(); } } } // If the file was altered during the processing then call the method // again to capture any new records. if (repeatProcess) ProcessSyncFile(); } }