简体   繁体   中英

Delaying an action for a period of time

I am currently working on a screensaver launcher, but I want to delay the activation of the screensaver for a period of time to ensure the mouse position is still in the position to activate it, to prevent accidental activation. I am thinking some kind of thread to check the position of the mouse for x secs before execution, would this be a sensible solution?

I'm not writing a screensaver, I have written an application so that when you move your mouse to a designated corner of your desktop, the screensaver comes on... I want to know how to add a delay to when you move the mouse into that corner to prevent accidental launching of the screensaver

If so how?

so that when you move your mouse to a designated corner of your desktop, the screensaver comes on...

When the mouse gets there, set a boolean and a timer. Make any mouse action clear the boolean.
Only when the timer fires and your flag is still there, proceed.

In other words, use a little state machine.

Here is pseudo code

   var CurrentMouse = ...; // Get mouse coordinates.
   ThreadPool.QueueUserWorkItem(        
   (s) => {
            Thread.Sleep(500); // Half a second

        //  if (Mouse did not move)
        //     Launch Screen Saver

   });

EDIT: Note this doesn't speak to invoking back to gui layer or thread locking. See the inspiration for this situation on my blog article entitled C# MultiThreading Using ThreadPool, Anonymous Delegates and Locks which does show proper locking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM