Skip to content

Commit

Permalink
move calculation of totals to a new thread
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveL-MSFT committed Dec 7, 2022
1 parent 7b30ceb commit 62757a0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ internal static bool IsMinimalProgressRenderingEnabled()

sb.Append(secRemain);

if (PercentComplete > 0 && PercentComplete < 100 && barWidth > 0)
if (PercentComplete >= 0 && PercentComplete < 100 && barWidth > 0)
{
int barLength = PercentComplete * barWidth / 100;
if (barLength >= barWidth)
Expand Down
22 changes: 12 additions & 10 deletions src/System.Management.Automation/namespaces/FileSystemProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Security;
using System.Security.AccessControl;
using System.Text;
using System.Threading;
using System.Xml;
using System.Xml.XPath;

Expand Down Expand Up @@ -3536,25 +3537,26 @@ protected override void CopyItem(
}
else // Copy-Item local
{
var progress = new ProgressRecord(COPY_FILE_ACTIVITY_ID, " ", " ");
if (Context != null)
{
if (Context.ExecutionContext.SessionState.PSVariable.Get(SpecialVariables.ProgressPreferenceVarPath.ToString()).Value is ActionPreference progressPreference)
{
if (progressPreference == ActionPreference.Continue)
{
progress.Activity = FileSystemProviderStrings.CollectingTotalActivity;
progress.PercentComplete = 0;
progress.RecordType = ProgressRecordType.Processing;
WriteProgress(progress);
GetTotalFiles(path, recurse);
// we use a thread to gather the total files so that copying can start right away
Thread t = new Thread(() =>
{
GetTotalFiles(path, recurse);
});
t.Start();
}
}
}

CopyItemLocalOrToSession(path, destinationPath, recurse, Force, null);
if (_totalFiles > 0)
{
var progress = new ProgressRecord(COPY_FILE_ACTIVITY_ID, " ", " ");
progress.RecordType = ProgressRecordType.Completed;
WriteProgress(progress);
}
Expand All @@ -3576,15 +3578,15 @@ private void GetTotalFiles(string path, bool recurse)
var dir = new DirectoryInfo(path);
foreach (var file in dir.EnumerateFiles())
{
_totalFiles++;
_totalBytes += file.Length;
Interlocked.Add(ref _totalFiles, 1);
Interlocked.Add(ref _totalBytes, file.Length);
}
}
else
{
var file = new FileInfo(path);
_totalFiles++;
_totalBytes += file.Length;
Interlocked.Add(ref _totalFiles, 1);
Interlocked.Add(ref _totalBytes, file.Length);
}
}
catch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,4 @@
<data name="CopyingLocalBytesStatus" xml:space="preserve">
<value>{0} MB of {1} MB</value>
</data>
<data name="CollectingTotalActivity" xml:space="preserve">
<value>Collecting total files</value>
</data>
</root>

0 comments on commit 62757a0

Please sign in to comment.