網(wǎng)站首頁 編程語言 正文
先給大家介紹下rust延遲5秒鎖屏的實現(xiàn)代碼:
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秒執(zhí)行以下程序
? ? 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
相關(guān)推薦
- 2022-04-05 Linux環(huán)境 redis 值中文顯示亂碼 解決辦法 --raw參數(shù)
- 2022-09-26 Josephus_problem_bidirectional 雙向約瑟夫問題
- 2023-07-27 使用Echarts圖表時,頁面切換后并且改變頁面窗口大小,再切回原來頁面Echarts圖表顯示有問題
- 2022-02-03 Windows10 可以上網(wǎng)(會斷)顯示無Internet鏈接
- 2022-09-30 詳解OpenCV執(zhí)行連通分量標記的方法和分析_python
- 2022-12-04 pytorch從頭開始搭建UNet++的過程詳解_相關(guān)技巧
- 2023-07-10 VMware三種網(wǎng)絡(luò)模式配置詳解。
- 2022-06-01 C語言超詳細解析函數(shù)棧幀_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支