#service #windows #receiver #toml #duration #mpsc #sync

winsrv

Windows服务的简单实现

1 个不稳定版本

0.0.1 2024年6月19日

#35 in #receiver

MIT 许可证

7KB
100 代码行(不包括注释)

Winsrv

Windows服务的简单实现

示例

在Cargo.toml中

[dependencies]
winsrv = { version = "0.0.1" }

然后在main.rs中

use std::sync::mpsc::{Receiver, RecvTimeoutError::Disconnected};
use std::time::Duration;
use winsrv::{run_service, SERVICE_CONTROL_SHUTDOWN, SERVICE_CONTROL_STOP};

fn srvmain(receiver: Receiver<u32>) {
    loop {
        match receiver.recv_timeout(Duration::from_secs(1)) {
            Ok(ctl) => match ctl {
                SERVICE_CONTROL_STOP | SERVICE_CONTROL_SHUTDOWN => break,
                _ => continue,
            },
            Err(err) => {
                if err == Disconnected {
                    break;
                }
            }
        };

        // Your code ...
    }
}

fn main() {
    run_service("Demo", srvmain);
}

许可证

本项目采用MIT许可证

依赖项

~128MB
~2M 估算额外代码行