/// <summary> /// Creates a new log file reader and adds it to the priority's log file enumerator. /// </summary> /// <param name="prio">The priority of files to write.</param> /// <param name="fileName">The name of the log file.</param> /// <param name="fromFsw">Indicates whether the reader was created from a FileSystemWatcher event.</param> private void AddNewReader(FieldLogPriority prio, string fileName, bool fromFsw) { // Must be within a lock(readerLock)! FL.Trace("AddNewReader, prio=" + prio + ", fileName=" + Path.GetFileName(fileName) + ", fromFsw=" + fromFsw); // Reject the new file if it's already in the queue (delayed FSW event after active scan) if (readers.ContainsKey(prio) && readers[prio] != null && readers[prio].ContainsFile(fileName)) { // This file is already current or queued FL.Checkpoint("This file is already current or queued"); return; } var reader = new FieldLogFileReader(fileName, true); ManualResetEvent h; if (!prioReadSignals.TryGetValue(prio, out h)) { h = new ManualResetEvent(false); prioReadSignals[prio] = h; } reader.ReadWaitHandle = h; if (!readers.ContainsKey(prio) || readers[prio] == null) { // This is the first file of this priority readers[prio] = new FieldLogFileEnumerator(reader); readers[prio].Error += FieldLogFileEnumerator_Error; readTasks[(int)prio] = Task <bool> .Factory.StartNew(readers[prio].MoveNext); // Signal the blocking ReadLogItem method that there's a new reader now newFilePrioEvent.Set(); } else { // Chain the new reader after the last reader in the queue readers[prio].Append(reader, fromFsw); // TODO,DEBUG: What for? //newFilePrioEvent.Set(); } }