I wrote a program to prevent the screen lock, using crate "rsautogui",
simplly move the mouse from 0,1080 to 100,1080 and sleep 10 seconds and then back to original position.
I run it on my computer ( company standard computer without admin right), but several minutes later it panic and stopped, very strange, because all code no need to unwrap.
Then I tried using crate "anyhow" , try to catch the error, but problem still happen.
code as below:
[dependencies]
rsautogui = "0.2.2"
anyhow = "1.0"
use rsautogui::mouse;
use std::thread::sleep;
use std::time::Duration;
use anyhow::{self, Ok};
fn run(x: u16) ->Result<(), anyhow::Error>{
let timeseconds = Duration::from_secs(5);
mouse::move_to(x, 1080);
sleep(timeseconds);
Ok(())
}
fn main() {
let mut x: i8 = -1;
loop {
if x > 0{
run(100_u16).unwrap_or_else(|_|{});
}else{
run(300_u16).unwrap_or_else(|_|{});
}
x = -x ;
}
}
my question: how to wrote code to avoid the panic?
(i already have another solution: not move the mouse but just send keys "Numlock" in a 30 seconds interval, and it's now running without any panics. I just want to know how to catch the panic for such situation, because all the code have no "unwrap", I think the code without unwrap should be no problem to run.)