Progress printing with threads?
Stanislav Blinov
stanislav.blinov at gmail.com
Wed Jul 1 08:36:32 UTC 2020
On Wednesday, 1 July 2020 at 07:52:28 UTC, AB wrote:
> Hello. I am unsure how to proceed about printing progress in my
> program.
> Is it a good idea to std.concurrency.spawn a new thread?..
> This example code shows my situation:
>
> MmFile input = new MmFile(/* ... */);
> ulong fileSize = input.length;
>
> for (ulong i = 0; i < fileSize; ++i)
> {
> // ...
> }
If you can only update the progress between iterations I don't
see why you would use threads here. A timer should suffice:
import std.datetime.stopwatch;
MmFile input = new MmFile(/* ... */);
ulong fileSize = input.length;
auto sw = StopWatch(AutoStart.yes);
for (ulong i = 0; i < fileSize; ++i)
{
// ...
if (sw.peek >= 2.seconds)
{
writefln("Progress: %5.2f%%", i*100.0/fileSize);
sw.reset;
}
}
More information about the Digitalmars-d-learn
mailing list