2014-07-21 75 views
0

我想停止監視器停止睡眠(這是由公司策略驅動的)。以下是我正在使用的代碼移動鼠標以停止睡眠監視器

while (true) 
{ 
    this.Cursor = new Cursor(Cursor.Current.Handle); 
    Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50); 
    Cursor.Clip = new Rectangle(this.Location, this.Size); 

    System.Threading.Thread.Sleep(2000); 

    this.Cursor = new Cursor(Cursor.Current.Handle); 
    Cursor.Position = new Point(Cursor.Position.X + 50, Cursor.Position.Y + 50); 
    Cursor.Clip = new Rectangle(this.Location, this.Size); 
} 

我可以看到沒有While循環的鼠標移動。但是,雖然它只移動鼠標一次,然後限制鼠標移動到右側。

有沒有更好的方法來做到這一點?

+1

http://www.perlmonks.org/?node=xy+problem – EZI

+0

添加一個'Thread.Sleep(2000);'最後,你會看到這個舉動。但我不知道這是否會阻止顯示器進入睡眠狀態。 – Bolu

+0

顯示器已被您的公司策略設置爲睡眠狀態的原因有很多,打破這些策略聽起來像是baaaaad的想法。 – DavidG

回答

3

如果你想讓你的電腦保持清醒,不要移動鼠標,只要告訴你的程序,電腦必須保持清醒。移動鼠標是一個非常糟糕的做法。

public class PowerHelper 
{ 
    public static void ForceSystemAwake() 
    { 
     NativeMethods.SetThreadExecutionState(NativeMethods.EXECUTION_STATE.ES_CONTINUOUS | 
               NativeMethods.EXECUTION_STATE.ES_DISPLAY_REQUIRED | 
               NativeMethods.EXECUTION_STATE.ES_SYSTEM_REQUIRED | 
               NativeMethods.EXECUTION_STATE.ES_AWAYMODE_REQUIRED); 
    } 

    public static void ResetSystemDefault() 
    { 
     NativeMethods.SetThreadExecutionState(NativeMethods.EXECUTION_STATE.ES_CONTINUOUS); 
    } 
} 

internal static partial class NativeMethods 
{ 
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    public static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags); 

    [FlagsAttribute] 
    public enum EXECUTION_STATE : uint 
    { 
     ES_AWAYMODE_REQUIRED = 0x00000040, 
     ES_CONTINUOUS = 0x80000000, 
     ES_DISPLAY_REQUIRED = 0x00000002, 
     ES_SYSTEM_REQUIRED = 0x00000001 

     // Legacy flag, should not be used. 
     // ES_USER_PRESENT = 0x00000004 
    } 
} 

,然後,只需撥打ForceSystemAwake()時要保持清醒您的計算機,然後調用ResetSystemDefault()當你完成

+0

這可能適用於屏幕保護程序,但不適用於PowerSaver選項中的顯示器睡眠。 –

1

此方法每4分鐘將鼠標移動1個像素,它不會讓您的顯示器進入睡眠狀態。

using System; 
using System.Drawing; 
using System.Windows.Forms; 

static class Program 
{ 
    static void Main() 
    { 
     Timer timer = new Timer(); 
     // timer.Interval = 4 minutes 
     timer.Interval = (int)(TimeSpan.TicksPerMinute * 4 /TimeSpan.TicksPerMillisecond); 
     timer.Tick += (sender, args) => { Cursor.Position = new Point(Cursor.Position.X + 1, Cursor.Position.Y + 1); }; 
     timer.Start(); 
     Application.Run(); 
    } 
}