学无先后,达者为师

网站首页 编程语言 正文

rust延迟5秒锁屏的实现代码_相关技巧

作者:Nazorine   更新时间: 2022-11-10 编程语言

先给大家介绍下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);     // 程序终止时间
}

原文链接:https://www.cnblogs.com/Nazorine/p/16707506.html

栏目分类
最近更新