Skip to content

Commit 91d6182

Browse files
committed
Improve performance of watcher
1 parent bd3ffb1 commit 91d6182

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

Files/ViewModels/ItemViewModel.cs

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,8 +1714,10 @@ private void WatchForDirectoryChanges(string path, CloudDriveSyncStatus syncStat
17141714
return;
17151715
}
17161716

1717+
var hasSyncStatus = syncStatus != CloudDriveSyncStatus.NotSynced && syncStatus != CloudDriveSyncStatus.Unknown;
1718+
17171719
var cts = new CancellationTokenSource();
1718-
_ = Windows.System.Threading.ThreadPool.RunAsync((x) => ProcessOperationQueue(cts.Token));
1720+
_ = Windows.System.Threading.ThreadPool.RunAsync((x) => ProcessOperationQueue(cts.Token, hasSyncStatus));
17191721

17201722
aWatcherAction = Windows.System.Threading.ThreadPool.RunAsync((x) =>
17211723
{
@@ -1724,7 +1726,7 @@ private void WatchForDirectoryChanges(string path, CloudDriveSyncStatus syncStat
17241726
buff = new byte[4096];
17251727
int notifyFilters = FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SIZE;
17261728

1727-
if (syncStatus != CloudDriveSyncStatus.NotSynced && syncStatus != CloudDriveSyncStatus.Unknown)
1729+
if (hasSyncStatus)
17281730
{
17291731
notifyFilters |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
17301732
}
@@ -1804,7 +1806,7 @@ private void WatchForDirectoryChanges(string path, CloudDriveSyncStatus syncStat
18041806
Debug.WriteLine("Task exiting...");
18051807
}
18061808

1807-
private async void ProcessOperationQueue(CancellationToken cancellationToken)
1809+
private async void ProcessOperationQueue(CancellationToken cancellationToken, bool hasSyncStatus)
18081810
{
18091811
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
18101812
string returnformat = Enum.Parse<TimeStyle>(localSettings.Values[Constants.LocalSettings.DateTimeFormat].ToString()) == TimeStyle.Application ? "D" : "g";
@@ -1816,6 +1818,7 @@ private async void ProcessOperationQueue(CancellationToken cancellationToken)
18161818
const uint FILE_ACTION_RENAMED_NEW_NAME = 0x00000005;
18171819

18181820
var sampler = new IntervalSampler(200);
1821+
var updateList = new HashSet<string>();
18191822
bool anyEdits = false;
18201823

18211824
try
@@ -1825,6 +1828,8 @@ private async void ProcessOperationQueue(CancellationToken cancellationToken)
18251828
if (operationEvent.Wait(200, cancellationToken))
18261829
{
18271830
operationEvent.Reset();
1831+
1832+
18281833
while (operationQueue.TryDequeue(out var operation))
18291834
{
18301835
if (cancellationToken.IsCancellationRequested) break;
@@ -1839,7 +1844,7 @@ private async void ProcessOperationQueue(CancellationToken cancellationToken)
18391844
break;
18401845

18411846
case FILE_ACTION_MODIFIED:
1842-
await UpdateFileOrFolderAsync(operation.FileName);
1847+
updateList.Add(operation.FileName);
18431848
break;
18441849

18451850
case FILE_ACTION_REMOVED:
@@ -1861,9 +1866,20 @@ private async void ProcessOperationQueue(CancellationToken cancellationToken)
18611866
anyEdits = false;
18621867
}
18631868
}
1869+
1870+
foreach (var entry in updateList.ToList())
1871+
{
1872+
await UpdateFileOrFolderAsync(entry, hasSyncStatus);
1873+
updateList.Remove(entry);
1874+
1875+
if (sampler.CheckNow() && operationQueue.Any(i => i.Action != FILE_ACTION_MODIFIED))
1876+
{
1877+
break;
1878+
}
1879+
}
18641880
}
18651881

1866-
if (anyEdits && sampler.CheckNow())
1882+
if (anyEdits)
18671883
{
18681884
await OrderFilesAndFoldersAsync();
18691885
await ApplyFilesAndFoldersChangesAsync();
@@ -1959,26 +1975,22 @@ public ListedItem AddFileOrFolderFromShellFile(ShellFileItem item, string dateRe
19591975

19601976
private async Task AddFileOrFolderAsync(ListedItem item)
19611977
{
1962-
try
1963-
{
1964-
await enumFolderSemaphore.WaitAsync(semaphoreCTS.Token);
1965-
}
1966-
catch (OperationCanceledException)
1978+
if (item == null)
19671979
{
19681980
return;
19691981
}
19701982

19711983
try
19721984
{
1973-
if (item != null)
1974-
{
1975-
filesAndFolders.Add(item);
1976-
}
1985+
await enumFolderSemaphore.WaitAsync(semaphoreCTS.Token);
19771986
}
1978-
finally
1987+
catch (OperationCanceledException)
19791988
{
1980-
enumFolderSemaphore.Release();
1989+
return;
19811990
}
1991+
1992+
filesAndFolders.Add(item);
1993+
enumFolderSemaphore.Release();
19821994
}
19831995

19841996
private async Task AddFileOrFolderAsync(string fileOrFolderPath, string dateReturnFormat)
@@ -2015,13 +2027,10 @@ private async Task AddFileOrFolderAsync(string fileOrFolderPath, string dateRetu
20152027
listedItem = await Win32StorageEnumerator.GetFile(findData, Directory.GetParent(fileOrFolderPath).FullName, dateReturnFormat, Connection, addFilesCTS.Token);
20162028
}
20172029

2018-
if (listedItem != null)
2019-
{
2020-
filesAndFolders.Add(listedItem);
2021-
}
2030+
await AddFileOrFolderAsync(listedItem);
20222031
}
20232032

2024-
private async Task UpdateFileOrFolderAsync(ListedItem item)
2033+
private async Task UpdateFileOrFolderAsync(ListedItem item, bool hasSyncStatus = false)
20252034
{
20262035
IStorageItem storageItem = null;
20272036
if (item.PrimaryItemAttribute == StorageItemTypes.File)
@@ -2034,10 +2043,13 @@ private async Task UpdateFileOrFolderAsync(ListedItem item)
20342043
}
20352044
if (storageItem != null)
20362045
{
2037-
var syncStatus = await CheckCloudDriveSyncStatusAsync(storageItem);
2046+
var syncStatus = hasSyncStatus ? await CheckCloudDriveSyncStatusAsync(storageItem) : CloudDriveSyncStatus.NotSynced;
20382047
await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(async () =>
20392048
{
2040-
item.SyncStatusUI = CloudDriveSyncStatusUI.FromCloudDriveSyncStatus(syncStatus);
2049+
if (hasSyncStatus)
2050+
{
2051+
item.SyncStatusUI = CloudDriveSyncStatusUI.FromCloudDriveSyncStatus(syncStatus);
2052+
}
20412053

20422054
if (storageItem.IsOfType(StorageItemTypes.File))
20432055
{
@@ -2057,7 +2069,7 @@ await CoreApplication.MainView.DispatcherQueue.EnqueueAsync(async () =>
20572069
}
20582070
}
20592071

2060-
private async Task UpdateFileOrFolderAsync(string path)
2072+
private async Task UpdateFileOrFolderAsync(string path, bool hasSyncStatus = false)
20612073
{
20622074
try
20632075
{
@@ -2074,7 +2086,7 @@ private async Task UpdateFileOrFolderAsync(string path)
20742086

20752087
if (matchingItem != null)
20762088
{
2077-
await UpdateFileOrFolderAsync(matchingItem);
2089+
await UpdateFileOrFolderAsync(matchingItem, hasSyncStatus);
20782090
}
20792091
}
20802092
finally

0 commit comments

Comments
 (0)