Skip to main content

once_delayed_task/
once_delayed_task.rs

1use async_trait::async_trait;
2use minitimer::{TaskBuilder, TaskRunner};
3
4struct DelayedPrintTask {
5    message: String,
6}
7
8#[async_trait]
9impl TaskRunner for DelayedPrintTask {
10    type Output = ();
11
12    async fn run(&self) -> Result<Self::Output, Box<dyn std::error::Error + Send + Sync>> {
13        println!("Task executed: {}", self.message);
14        Ok(())
15    }
16}
17
18#[tokio::main]
19async fn main() {
20    let timer = minitimer::MiniTimer::new();
21
22    let task = TaskBuilder::new(1)
23        .with_frequency_once_by_seconds(3)
24        .spwan_async(DelayedPrintTask {
25            message: "Hello from minitimer!".to_string(),
26        })
27        .unwrap();
28
29    timer.add_task(task).unwrap();
30
31    println!("Timer started. Waiting 5 seconds...");
32    tokio::time::sleep(std::time::Duration::from_secs(5)).await;
33
34    println!("Example completed.");
35}