diff --git a/src/Renci.SshNet/SftpClient.cs b/src/Renci.SshNet/SftpClient.cs index aa30efadb..01472dbff 100644 --- a/src/Renci.SshNet/SftpClient.cs +++ b/src/Renci.SshNet/SftpClient.cs @@ -583,7 +583,7 @@ public IEnumerable ListDirectory(string path, Action listCallbac { CheckDisposed(); - return InternalListDirectory(path, listCallback); + return InternalListDirectory(path, asyncResult: null, listCallback); } /// @@ -666,12 +666,7 @@ public IAsyncResult BeginListDirectory(string path, AsyncCallback asyncCallback, { try { - var result = InternalListDirectory(path, count => - { - asyncResult.Update(count); - - listCallback?.Invoke(count); - }); + var result = InternalListDirectory(path, asyncResult, listCallback); asyncResult.SetAsCompleted(result, completedSynchronously: false); } @@ -898,12 +893,7 @@ public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback { try { - InternalDownloadFile(path, output, asyncResult, offset => - { - asyncResult.Update(offset); - - downloadCallback?.Invoke(offset); - }); + InternalDownloadFile(path, output, asyncResult, downloadCallback); asyncResult.SetAsCompleted(exception: null, completedSynchronously: false); } @@ -1131,11 +1121,7 @@ public IAsyncResult BeginUploadFile(Stream input, string path, bool canOverride, { try { - InternalUploadFile(input, path, flags, asyncResult, offset => - { - asyncResult.Update(offset); - uploadCallback?.Invoke(offset); - }); + InternalUploadFile(input, path, flags, asyncResult, uploadCallback); asyncResult.SetAsCompleted(exception: null, completedSynchronously: false); } @@ -2200,7 +2186,7 @@ private List InternalSynchronizeDirectories(string sourcePath, string #region Existing Files at The Destination - var destFiles = InternalListDirectory(destinationPath, listCallback: null); + var destFiles = InternalListDirectory(destinationPath, asyncResult: null, listCallback: null); var destDict = new Dictionary(); foreach (var destFile in destFiles) { @@ -2268,13 +2254,14 @@ private List InternalSynchronizeDirectories(string sourcePath, string /// Internals the list directory. /// /// The path. + /// An that references the asynchronous request. /// The list callback. /// /// A list of files in the specfied directory. /// /// is . /// Client not connected. - private List InternalListDirectory(string path, Action listCallback) + private List InternalListDirectory(string path, SftpListDirectoryAsyncResult asyncResult, Action listCallback) { if (path is null) { @@ -2314,6 +2301,8 @@ private List InternalListDirectory(string path, Action listCallb f.Value)); } + asyncResult?.Update(result.Count); + // Call callback to report number of files read if (listCallback is not null) { @@ -2380,6 +2369,8 @@ private void InternalDownloadFile(string path, Stream output, SftpDownloadAsyncR totalBytesRead += (ulong) data.Length; + asyncResult?.Update(totalBytesRead); + if (downloadCallback is not null) { // Copy offset to ensure it's not modified between now and execution of callback @@ -2452,6 +2443,8 @@ private void InternalUploadFile(Stream input, string path, Flags flags, SftpUplo _ = Interlocked.Decrement(ref expectedResponses); _ = responseReceivedWaitHandle.Set(); + asyncResult?.Update(writtenBytes); + // Call callback to report number of bytes written if (uploadCallback is not null) {