bloomtom.NaiveProgress
1.0.0
dotnet add package bloomtom.NaiveProgress --version 1.0.0
NuGet\Install-Package bloomtom.NaiveProgress -Version 1.0.0
<PackageReference Include="bloomtom.NaiveProgress" Version="1.0.0" />
paket add bloomtom.NaiveProgress --version 1.0.0
#r "nuget: bloomtom.NaiveProgress, 1.0.0"
// Install bloomtom.NaiveProgress as a Cake Addin
#addin nuget:?package=bloomtom.NaiveProgress&version=1.0.0
// Install bloomtom.NaiveProgress as a Cake Tool
#tool nuget:?package=bloomtom.NaiveProgress&version=1.0.0
NaiveProgress
Provides a non-threaded implementation of
IProgress
.
The canonical implementation of IProgress
is Progress
, which uses a SynchronizationContext
if you have one, and the thread pool if you don't. ASP.NET Core, Console and Test applications don't. The result is your progress reports might end up coming in out-of-order.
NaiveProgress
doesn't do anything fancy. No SynchronizationContext
, no thread pool. All reports are passed along to an event in the order they are received.
Nuget Packages
Package Name | Target Framework | Version |
---|---|---|
NaiveProgress | .NET Standard 2.0 |
Usage
Whenever you need an IProgress
, make a NaiveProgress<T>
instead of a Progress<T>
; it was designed to be a drop in replacement.
Proof of Concept
Consider this function which delays for a period of time, and frequently reports progress.
private static void Delay(TimeSpan t, IProgress<TimeSpan> progress)
{
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
while (sw.Elapsed < t)
{
System.Threading.Thread.Sleep(1);
progress.Report(sw.Elapsed);
}
}
Using the normal Progress
implementation is as follows
var progress = new Progress<TimeSpan>((e) =>
{
Console.WriteLine(e.Ticks);
});
Delay(TimeSpan.FromMilliseconds(20), progress);
The following is the entire real output from running the above in a console application.
12618
468586
69860
117134
Ouch. As you can see, the console writes are out-of-order. Otherwise you'd expect Ticks to always increase.
To get in-order reports, simply change Progress
to NaiveProgress
var progress = new NaiveProgress<TimeSpan>((e) =>
{
Console.WriteLine(e.Ticks);
});
Delay(TimeSpan.FromMilliseconds(20), progress);
The result:
12986
62947
78007
93076
108074
123171
138230
153266
168358
183371
198416
213470
Every event is handled, and they're handled in-order.
Product | Versions |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard2.0 netstandard2.1 |
.NET Framework | net461 net462 net463 net47 net471 net472 net48 net481 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen40 tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 679 | 1/4/2019 |