diff --git a/ShareX.HelpersLib/Helpers/Helpers.cs b/ShareX.HelpersLib/Helpers/Helpers.cs index 930ce3502..d5861adc0 100644 --- a/ShareX.HelpersLib/Helpers/Helpers.cs +++ b/ShareX.HelpersLib/Helpers/Helpers.cs @@ -828,6 +828,19 @@ public static bool IsFileLocked(string path) return false; } + public static long GetFileSize(string path) + { + try + { + return new FileInfo(path).Length; + } + catch + { + } + + return -1; + } + public static void CreateDirectoryFromDirectoryPath(string path) { if (!string.IsNullOrEmpty(path) && !Directory.Exists(path)) diff --git a/ShareX/WatchFolder.cs b/ShareX/WatchFolder.cs index b95b9220a..a664242ce 100644 --- a/ShareX/WatchFolder.cs +++ b/ShareX/WatchFolder.cs @@ -86,8 +86,30 @@ private void fileWatcher_Created(object sender, FileSystemEventArgs e) timers.Add(new WatchFolderDuplicateEventTimer(path)); - Action onCompleted = () => context.Post(state => OnFileWatcherTrigger(path), null); - Helpers.WaitWhileAsync(() => Helpers.IsFileLocked(path), 250, 5000, onCompleted, 1000); + int successCount = 0; + long previousSize = -1; + + Helpers.WaitWhileAsync(() => + { + if (!Helpers.IsFileLocked(path)) + { + long currentSize = Helpers.GetFileSize(path); + + if (currentSize > 0 && currentSize == previousSize) + { + successCount++; + } + + previousSize = currentSize; + return successCount < 4; + } + + previousSize = -1; + return true; + }, 250, 5000, () => + { + context.Post(state => OnFileWatcherTrigger(path), null); + }, 1000); } protected void CleanElapsedTimers()