日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

rust延遲5秒鎖屏的實現(xiàn)代碼_相關(guān)技巧

作者:Nazorine ? 更新時間: 2022-11-10 編程語言

先給大家介紹下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

欄目分類
最近更新