Skip to content

Add IncrementMaxValue method to ProgressTask for thread safety #1893

@RJohnsonPGH

Description

@RJohnsonPGH

Is your feature request related to a problem? Please describe.
A feature.

Updating the MaxValue property is not thread safe, and no thread safe method is available to do this. This is a common scenario where I might be 'discovering' any number of items that the task then has to perform some operation on.

This will lose progress updates:

var progress = new Progress<DiscoveryProgress>(count =>
{
    someTask.MaxValue += count;
}
await someParallelMethod(progress);

A workaround for this is:

var maxValue = 0;
var progress = new Progress<DiscoveryProgress>(count =>
{
    someTask.MaxValue = Interlocked.Add(maxValue, count);
}
await someParallelMethod(progress);

Which is functional, but can become a problem with many tasks.

However, thread safety is already in place for updating the Value property via Increment.

    public void Increment(double value)
    {
        Update(increment: value);
    }

    private void Update(
        string? description = null,
        double? maxValue = null,
        double? increment = null,
        double? value = null)
    {
        lock (_lock)
        {
            // Omitted code here
        }
    }

Update is already thread safe, and even has the capability to update the MaxValue property, but is inaccessible due to it being private.

Describe the solution you'd like
An additional public method in ProgressTask: IncrementMaxValue that uses the existing private Update method to allow for thread safe updates to MaxValue.

Describe alternatives you've considered
Workaround described above.

Additional context
none


Please upvote 👍 this issue if you are interested in it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Todo 🕑

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions