rust延迟5秒锁屏的实现代码
更新时间:2022年09月19日 14:43:44 作者:Nazorine
这篇文章主要介绍了rust延迟5秒锁屏的实现代码,文中通过实例代码也介绍了rust计算程序运行时间的方法,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud)
(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun)
先给大家介绍下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); // 程序终止时间 }
到此这篇关于rust延迟5秒锁屏的实现代码的文章就介绍到这了,更多相关rust延迟锁屏内容请搜索程序员之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持程序员之家!
相关文章
Rust开发WebAssembly在Html和Vue中的应用小结(推荐)
这篇文章主要介绍了Rust开发WebAssembly在Html和Vue中的应用,本文将带领大家在普通html上和vue手脚架上都来运行wasm的流程,需要的朋友可以参考下2022-08-08
最新评论