Из вашего описания процесса вы можете использовать Список (из строки) или Очередь (из строки) для хранения последних N
строк, прочитанных из журналаfile.
A List(of String)
, вероятно, немного быстрее, чем Queue(Of String)
, если производительность вызывает сомнения.
Пример с использованием двух классов:
Примечание: при обнаружении ошибки список (lineStrings
) будет содержать MaxElements + 1
элементов.Последняя строка содержит ошибку, остальные MaxElements
строки содержат описание ошибки или что-то еще.
Dim MaxElements As Integer = 10
Dim lineStrings As List(Of String) = new List(of String)
'(...)
'Open the file stream using a StreamReader
lineStrings.Add(readfile.ReadLine())
If lineStrings.Last().Contains([Error]) Then
'Process the error using the lineStrings items
'Clear the List if the content is not required anymore
lineStrings.Clear()
End If
If lineStrings.Count > MaxElements Then lineStrings.RemoveAt(0)
Вы можете сделать то же самое с Queue(Of String)
:
Dim lineStrings As Queue(Of String) = new Queue(of String)(MaxElements)
lineStrings.Enqueue(readfile.ReadLine())
If lineStrings.Last().Contains([Error]) Then
'Process the error using the lineStrings items
'Clear the Queue if needed/preferable
lineStrings.Clear()
End If
If lineStrings.Count > MaxElements Then lineStrings.Dequeue()
Тест скорости, добавление элементов 100,000
и 1,000,000
, проверка на ошибки и удаление 10-го элемента из списка, без обработки (скомпилировано как выпуск с использованием файла .exe):
100,000 elements (average):
List(Of String): 21 ms
Queue(Of String): 29 ms
1,000,000 elements (average):
List(Of String): 206 ms
Queue(Of String): 298 ms