Redpoint.ProgressMonitor 2023.162.418

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Redpoint.ProgressMonitor --version 2023.162.418                
NuGet\Install-Package Redpoint.ProgressMonitor -Version 2023.162.418                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Redpoint.ProgressMonitor" Version="2023.162.418" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Redpoint.ProgressMonitor --version 2023.162.418                
#r "nuget: Redpoint.ProgressMonitor, 2023.162.418"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Redpoint.ProgressMonitor as a Cake Addin
#addin nuget:?package=Redpoint.ProgressMonitor&version=2023.162.418

// Install Redpoint.ProgressMonitor as a Cake Tool
#tool nuget:?package=Redpoint.ProgressMonitor&version=2023.162.418                

Redpoint.ProgressMonitor

This library provides APIs for monitoring and reporting the progress of arbitrary operations in console applications.

Read on for the following examples:

Example for a generic stream

You can monitor an operation that uses a stream like so:

// Inject these services.
IProgressFactory _progressFactory;
IMonitorFactory _monitorFactory;

using (var stream = new FileStream(...))
{
    // Start monitoring.
    var cts = new CancellationTokenSource();
    var progress = _progressFactory.CreateProgressForStream(stream);
    var monitorTask = Task.Run(async () =>
    {
        var consoleWidth = 0;
        try
        {
            consoleWidth = Console.BufferWidth;
        }
        catch
        {
            // Not connected to a console, e.g. output is
            // redirected.
        }

        var monitor = _monitorFactory.CreateByteBasedMonitor();
        await monitor.MonitorAsync(
            progress,
            null,
            (message, count) =>
            {
                if (consoleWidth != 0)
                {
                    // Emit the progress information in such a
                    // way that we overwrite the previous info
                    // reported to the console.
                    Console.Write($"\r{message}".PadRight(consoleWidth));
                }
                else
                {
                    // Emit onto a new line every 5 seconds. This
                    // callback is invoked every 100ms.
                    if (count % 50 == 0)
                    {
                        Console.WriteLine(message);
                    }
                }
            },
            cts.Token);
    });

    // e.g. hash the stream.
    byte[] hashBytes;
    using (var hasher = SHA256.Create())
    {
        hashBytes = await hasher.ComputeHashAsync(stream);
    }

    // Stop monitoring.
    cts.Cancel();
    try
    {
        await monitorTask;
    }
    catch (OperationCanceledException) { }

    // Emit a newline after our progress message.
    if (consoleWidth != 0)
    {
        Console.WriteLine();
    }
}

Example for a HTTP download

If you're reporting progress on a HTTP stream, there's a few extra things to keep in mind:

  • You need to pass HttpCompletionOption.ResponseHeadersRead as the completion option, or HttpClient will buffer the entire response by default.
  • You need to wrap the stream you read from in PositionAwareStream, which is a class provided by this library. Since the underlying HTTP stream does not support Position or Length, this wrapping stream tracks the position as the stream is read from and allows the length to be passed in as a constructor parameter (which you should set based on the Content-Length header).

Below is a concise example of how to show the progress of downloading a file:

using (var client = new HttpClient())
{
    using (var target = new FileStream(targetPath, FileMode.Create, FileAccess.Write, FileShare.None))
    {
        var response = await client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead);
        using (var stream = new PositionAwareStream(
            await response.Content.ReadAsStreamAsync(),
            response.Content.Headers.ContentLength!.Value))
        {
            var cts = new CancellationTokenSource();
            var progress = _progressFactory.CreateProgressForStream(stream);
            var monitorTask = Task.Run(async () =>
            {
                var consoleWidth = 0;
                try
                {
                    consoleWidth = Console.BufferWidth;
                }
                catch { }

                var monitor = _monitorFactory.CreateByteBasedMonitor();
                await monitor.MonitorAsync(
                    progress,
                    null,
                    (message, count) =>
                    {
                        if (consoleWidth != 0)
                        {
                            Console.Write($"\r{message}".PadRight(consoleWidth));
                        }
                        else if (count % 50 == 0)
                        {
                            Console.WriteLine(message);
                        }
                    },
                    cts.Token);
            });

            await stream.CopyToAsync(target);

            cts.Cancel();
            try
            {
                await monitorTask;
            }
            catch (OperationCanceledException) { }
        }
    }
}
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Redpoint.ProgressMonitor:

Package Downloads
Redpoint.PackageManagement

Provides APIs for installing, upgrading and uninstalling packages with WinGet and Homebrew.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2023.1176.407 182 6/25/2023
2023.1176.396 148 6/25/2023
2023.1176.363 159 6/25/2023
2023.1176.360 192 6/25/2023
2023.1175.638 153 6/24/2023
2023.1170.907 147 6/19/2023
2023.1170.900 152 6/19/2023
2023.1167.562 148 6/16/2023
2023.1167.556 152 6/16/2023
2023.1167.496 163 6/16/2023
2023.1166.1008 166 6/15/2023
2023.1166.938 140 6/15/2023
2023.1166.713 161 6/15/2023
2023.1166.699 147 6/15/2023
2023.1165.1065 155 6/14/2023
2023.1165.888 154 6/14/2023
2023.1165.878 156 6/14/2023
2023.1165.861 143 6/14/2023
2023.1165.828 157 6/14/2023
2023.1165.686 170 6/14/2023
2023.1165.653 150 6/14/2023
2023.377.1003 232 5/31/2023
2023.377.909 169 5/31/2023
2023.377.558 169 5/31/2023
2023.365.1417 217 5/30/2023
2023.365.1350 155 5/30/2023
2023.365.1327 151 5/30/2023
2023.365.1306 167 5/30/2023
2023.365.1198 179 5/30/2023
2023.365.1046 156 5/30/2023
2023.365.710 155 5/30/2023
2023.365.703 167 5/30/2023
2023.365.336 158 5/30/2023
2023.174.636 157 6/14/2023
2023.174.616 139 6/14/2023
2023.174.442 150 6/14/2023
2023.162.1243 143 6/13/2023
2023.162.1225 150 6/13/2023
2023.162.1023 139 6/13/2023
2023.162.948 143 6/13/2023
2023.162.865 153 6/13/2023
2023.162.770 172 6/13/2023
2023.162.734 167 6/13/2023
2023.162.701 157 6/13/2023
2023.162.470 169 6/13/2023
2023.162.418 151 6/13/2023
2023.150.1142 152 6/12/2023
2023.150.1121 154 6/12/2023
2023.150.1095 150 6/12/2023
2023.150.1081 139 6/12/2023
2023.150.1066 150 6/12/2023
2023.150.999 140 6/12/2023
2023.150.916 168 6/12/2023
2023.150.865 154 6/12/2023
2023.150.831 152 6/12/2023
2023.150.774 148 6/12/2023
2023.150.613 145 6/12/2023
2023.150.558 161 6/12/2023
2023.150.456 175 6/12/2023
2023.150.450 176 6/12/2023
2023.150.220 172 6/12/2023
2023.150.179 179 6/12/2023
2023.150.167 174 6/12/2023
2023.138.864 165 6/11/2023
2023.138.837 160 6/11/2023
2023.138.363 170 6/11/2023
2023.138.224 196 6/11/2023
2023.138.213 145 6/11/2023
2023.126.1167 173 6/11/2023
2023.126.1148 166 6/10/2023
2023.126.838 176 6/10/2023
2023.126.794 171 6/10/2023
2023.126.745 173 6/10/2023
2023.126.714 184 6/10/2023
2023.126.662 180 6/10/2023
2023.114.544 154 6/9/2023
2023.114.351 166 6/9/2023
2023.90.1030 171 6/7/2023
2023.90.1009 154 6/7/2023
2023.54.1152 153 6/4/2023
2023.54.419 169 6/4/2023
2023.54.198 154 6/4/2023
2023.54.60 137 6/4/2023
2023.54.48 163 6/4/2023
2023.42.745 155 6/3/2023
2023.30.1191 149 6/2/2023
2023.30.1172 169 6/2/2023
2023.30.1163 156 6/2/2023
2023.30.1147 165 6/2/2023
2023.30.1136 175 6/2/2023
2023.30.765 176 6/2/2023
2023.30.761 162 6/2/2023
2023.30.747 179 6/2/2023
2023.30.734 150 6/2/2023