How to update Util.Progress from separate thread?

JeffMercado
edited May 2024 in General

I'm invoking some command line processes asynchronously using the CliWrap library. As it's running, I want to try to keep track of the relative progress and use Util.Progress to do that. However it's not working for me, I'm assuming because the thread I'm getting updates on is different from the main one. The actual progress I see in LINQPad doesn't update like I would expect.

How can I get this updated? Is there an "Invoke()" method that I can call?

Util.Progress = 0;
await Cli.Wrap(@"path to some process")
    .WithArguments([my arguments])
    .WithStandardOutputPipe(PipeTarget.ToDelegate(ProcessStdout)) // calls delegate for every line written to stdout
    .ExecuteAsync(QueryCancelToken);
Util.Progress = null;

void ProcessStdout(string line)
{
    // parse line and determine if any progress has been made
    if (/* not a progress line */)
    {
        line.Dump(); // output like normal
        return;
    }
    // calculate the progress
    var progress = /* some number between 0 and 100 */;
    Util.Progress = progress; // doesn't actually update the progress
}

Comments

  • JoeAlbahari
    edited May 2024

    Util.Progress is threadsafe. Could there be something else causing your issue? Have you tried logging the updates to Util.Progress?

  • Facepalm. My mistake... the progress calculation was comparing wrong time units.

                var progress = ts.TotalMilliseconds*100 / duration.TotalMicroseconds;
    

    Didn't realize the wrong units were being used. Fixing it fixed the progress.