rust延迟锁屏
Nazorine 人气:0先给大家介绍下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); // 程序终止时间 }
加载全部内容