How to avoid panic in such situation?

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.)

What is the panic? The stack trace should be logged when it happens. That would help track down the source.

Looks like rsautogui panics instead of returning errors.

You can look for another crate that doesn't panic, or you could catch the panic.

I built it with .exe format, and run in cmd,
the error message:
failed to move (just one sentence, something like these words. i already run the new program,)

I have the idea, because within the create, it didn't handle the panic and propagate the panic to outside.
thanks.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.