Asynchron

Crates.io Asynchron documentation CI

Asynchronize blocking operation.

Example

```rust use asynchron::{Futurize, Futurized, ITaskHandle, Progress}; use std::time::{Duration, Instant};

fn main() { let instant: Instant = Instant::now(); let task: Futurized = Futurize::task( 0, move |task: ITaskHandle| -> Progress { let sleepdur = Duration::frommillis(10); std::thread::sleep(sleepdur); // Send current task progress. let result = Ok::

        if _task.is_canceled() {
            _task.send("Canceling the task".into());
            return Progress::Canceled;
        }
        Progress::Completed(instant.elapsed().subsec_millis())
    },
);

// Try do the task now.
task.try_do();

let mut exit = false;
loop {
    task.try_resolve(|progress, resolved| {
        match progress {
            Progress::Current(task_receiver) => {
                if let Some(value) = task_receiver {
                    println!("{}\n", value)
                }
                // // Cancel if need to.
                // task.cancel()
            }
            Progress::Canceled => {
                println!("The task was canceled\n")
            }
            Progress::Completed(elapsed) => {
                println!("The task finished in: {:?} milliseconds\n", elapsed)
            }
            Progress::Error(e) => {
                println!("{}\n", e)
            }
        }

        if resolved {
            // This scope act like "finally block", do final things here.
            exit = true
        }
    });

    if exit {
        break;
    }
}

} ```

More Example

Mixing sync and async with tokio, reqwest and fltk-rs can be found here.