/// Actions async Task InitialScan() { var stopwatch = Stopwatch.StartNew(); ReportLine("Started initial scan\n"); StorageItemQueryResult query = CreateQuery(); try { using (await _lock.LockAsync()) { int itemIndex = 0; uint chunkIndex = 0; uint chunkSize = 50; var chunkItems = new List <MonitoredFolderItem>((int)chunkSize); int addedCount = 0; do { if (_cancellationToken.IsCancellationRequested) { break; } // Get items var items = await query.GetItemsAsync(chunkIndex, chunkSize); if (!items.Any()) { break; } Report("."); // Gather added items chunkItems.Clear(); foreach (var item in items) { if (_cancellationToken.IsCancellationRequested) { break; } var itemName = item.Name; var newItem = new MonitoredFolderItem(item); _cachedItems.Insert(itemIndex, newItem); chunkItems.Add(newItem); itemIndex++; } if (_cancellationToken.IsCancellationRequested) { break; } if (chunkItems.Any()) { addedCount += chunkItems.Count; // Raise event Changed?.Invoke(this, new MonitoredFolderChangedArgs(chunkItems)); } // Move Next chunkIndex += (uint)items.Count(); }while (true); stopwatch.Stop(); ReportLine($"+{addedCount}"); ReportLine($"Finished initial scan. Processed {chunkIndex} items in {stopwatch.ElapsedMilliseconds} ms."); ReportLine("Releasing Lock"); } } catch (Exception ex) { ReportLine($"Exception\n{ex}"); } }
async Task Scan() { var scanId = ++_debugScanCount; ReportLine(scanId, "Started"); try { using (await _lock.LockAsync()) { _isScanQueued = false; ReportLine(scanId, "Obtained Lock\n"); var stopwatch = Stopwatch.StartNew(); var query = CreateQuery(); uint chunkIndex = 0; uint chunkSize = 50; var chunkItemsAdded = new List <MonitoredFolderItem>((int)chunkSize); var chunkItemsRemoved = new List <MonitoredFolderItem>((int)chunkSize); int addedCount = 0; int removedCount = 0; int thisIndex = -1; int existingIndex = -1; var existingItems = _cachedItems.ToList(); do { if (_cancellationToken.IsCancellationRequested) { break; } var items = await query.GetItemsAsync(chunkIndex, chunkSize); if (!items.Any()) { break; } Report("."); foreach (var item in items) { if (_cancellationToken.IsCancellationRequested) { break; } thisIndex++; var thisItem = new MonitoredFolderItem(item); existingIndex++; if (existingIndex < existingItems.Count) { var existingItem = existingItems[existingIndex]; if (existingItem.Equals(thisItem)) { continue; } var comparison = existingItem.CompareTo(thisItem); // Delete all existingItems at this index < thisItem while (comparison < 0) { existingItems.RemoveAt(existingIndex); chunkItemsRemoved.Add(existingItem); if (existingItems.Count == existingIndex) { break; } existingItem = existingItems[existingIndex]; comparison = existingItem.CompareTo(thisItem); } if (existingIndex == existingItems.Count) { existingItems.Add(thisItem); chunkItemsAdded.Add(thisItem); } else if (comparison > 0) { existingItems.Insert(existingIndex, thisItem); chunkItemsAdded.Add(thisItem); } } else { existingItems.Add(thisItem); chunkItemsAdded.Add(thisItem); } } if (_cancellationToken.IsCancellationRequested) { break; } // Raise event if (chunkItemsAdded.Any() || chunkItemsRemoved.Any()) { await UIThread.InvokeAsync(() => { Changed?.Invoke(this, new MonitoredFolderChangedArgs(chunkItemsAdded, chunkItemsRemoved)); }); addedCount += chunkItemsAdded.Count; removedCount += chunkItemsRemoved.Count; chunkItemsAdded.Clear(); chunkItemsRemoved.Clear(); } // Move Next chunkIndex += (uint)items.Count(); }while (true); // Any remaining existingItems are removed if (existingIndex < existingItems.Count - 1) { var itemsToRemove = existingItems.Where((_, i) => i > existingIndex).ToList(); chunkItemsRemoved.Clear(); chunkItemsRemoved.AddRange(itemsToRemove); removedCount += itemsToRemove.Count; var numberToRemove = existingItems.Count - 1 - existingIndex; existingItems.RemoveRange(existingItems.Count - numberToRemove, numberToRemove); await UIThread.InvokeAsync(() => { Changed?.Invoke(this, new MonitoredFolderChangedArgs(null, chunkItemsRemoved)); }); } // Result _cachedItems = existingItems; stopwatch.Stop(); if (chunkIndex > 0) { ReportLine($"+{addedCount} -{removedCount}"); } ReportLine(scanId, $"Finished. Processed {chunkIndex} items in {stopwatch.ElapsedMilliseconds} ms."); ReportLine("Releasing Lock"); } } catch (Exception ex) { ReportLine(scanId, $"Exception\n{ex}"); } }