rust延迟锁屏

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

rust延迟锁屏

Nazorine   2022-09-29 我要评论

先给大家介绍下rust延迟5秒锁屏的实现代码:

main.rs

#![windows_subsystem = "windows"]
use std::process::Command;
use std::os::windows::process::CommandExt;
use std::thread::sleep;
use std::time::Duration;

fn main() {

    let time_seconds = Duration::from_secs(5);
    sleep(time_seconds); // 延迟5秒执行以下程序

    let output = if cfg!(target_os = "windows") {
        Command::new("cmd")
                .creation_flags(0x08000000)
                .arg("/C")
                .arg("Rundll32.exe user32.dll,LockWorkStation")
                .output()
                .expect("failed to execute process")
    } else {
        Command::new("sh")
                .arg("-c")
                .arg("echo hello")
                .output()
                .expect("failed to execute process")
    };
    
    let hello = output.stdout;
    println!("{:?}", hello);

}

扩展知识:下面看下rust计算程序运行时间

main.rs

use std::thread::sleep;
use std::time::{Duration,Instant};
fn main() {
    let now = Instant::now();  // 程序起始时间
    println!("{:?}",now);  

    let three_seconds = Duration::from_secs(3);
    sleep(three_seconds);  // 延迟3秒

    let end = now.elapsed().as_secs();
    println!("程序运行了 {:?} 秒",end);     // 程序终止时间
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们