Crate background_runner

Crate background_runner 

Source
Expand description

This crate provides the BackgroundRunner struct, which runs a given task in the background once BackgroundRunner::update() is called.

use background_runner::BackgroundRunner;
use std::thread::sleep;
use std::time::Duration;
use std::time::Instant;

let mut runner_iter = 0;

// Set up the runner, giving it a task to run
let runner = BackgroundRunner::new(move |iter| {
    // Simulate some heavy work
    println!("runner_iter = {runner_iter}, iter = {iter}");
    runner_iter += 1;
    sleep(Duration::from_millis(10));
});

let start = Instant::now();
let mut iter = 0;
while start.elapsed().as_millis() < 100 {
    // Update the runner with the current loop iteration
    runner.update(&iter);
    iter += 1;
}

Structsยง

BackgroundRunner
Runner for a background task to be invoked periodically.