none
Stop screensaver from coming on

Answers

  • Ah, so at last we find the root of the problem. It is not the screen saver itself, but the fact that the connection between the application and the device dies in certain situations. Clearly the solution is not to spam the OS with simulated keyboard pressing, or even to disable the screen saver.

    My guess is that it has nothing to do with the screen saver, but rather is either a result of the computer going to sleep or the workstation being locked, more likely the latter. For that, you can listen for changes. Let me whip up a quick sample (never tried this in a managed app)...

    Oh and as for reproducing the problem, my best advice is to download Virtual PC, install XP and Server X, and now (hopefully) you can simulate the same situation.
    • Proposed as answer by Figo Fei Friday, May 29, 2009 8:00 AM
    • Marked as answer by Figo Fei Monday, June 01, 2009 3:41 AM
    Wednesday, May 27, 2009 9:27 PM
  • I attempted to add power suspend/resume to that componant, but it seems that message windows don't get the message. So going back to the first sample, the following should be all you need to suspend and resume communications with your device:

    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    public class Form1 : Form
    {
        static void Main()
        {
            Application.Run(new Form1());
        }
    
        [DllImport("wtsapi32")]
        private static extern bool WTSRegisterSessionNotification(HandleRef hwnd, int flag);
        [DllImport("wtsapi32")]
        private static extern bool WTSUnRegisterSessionNotification(HandleRef hwnd);
    
        const int NOTIFY_FOR_THIS_SESSION = 0;
        const int NOTIFY_FOR_ALL_SESSIONS = 1;
    
        private ListBox listBox;
        public Form1()
        {
            listBox = new ListBox();
            listBox.Dock = DockStyle.Fill;
            Controls.Add(listBox);
        }
    
        bool wtsRegistered = false;
    
        protected override void OnHandleCreated(EventArgs e)
        {
            wtsRegistered = WTSRegisterSessionNotification(new HandleRef(this, Handle), NOTIFY_FOR_THIS_SESSION);
            Text = String.Format("WTS Registered : {0}", wtsRegistered);
            base.OnHandleCreated(e);
        }
    
        protected override void OnHandleDestroyed(EventArgs e)
        {
            if (wtsRegistered)
                WTSUnRegisterSessionNotification(new HandleRef(this, Handle));
            base.OnHandleDestroyed(e);
        }
    
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x02B1) // WM_WTSSESSION_CHANGE
            {
                switch ((int)m.WParam)
                {
                    case 7: // WTS_SESSION_LOCK:
                        listBox.Items.Add("Session locked.");
                        break;
                    case 8: // WTS_SESSION_UNLOCK:
                        listBox.Items.Add("Session Unlocked.");
                        break;
                }
                return;
            }
            else if (m.Msg == 0x0218) // WM_POWERBROADCAST
            {
                switch ((int)m.WParam)
                {
                    case 0x04: // PBT_APMSUSPEND
                        listBox.Items.Add("Power suspended.");
                        break;
                    case 0x12: // PBT_APMRESUMEAUTOMATIC
                        listBox.Items.Add("Power resumed.");
                        break;
                }
            }
            base.WndProc(ref m);
        }
    }
    
    • Proposed as answer by Figo Fei Friday, May 29, 2009 7:41 AM
    • Marked as answer by ScottyDoesKnow Monday, June 15, 2009 7:46 PM
    Thursday, May 28, 2009 2:56 PM

All replies

  • You can  kill the screen saver process.  Create timer that monitor your process and if screen saver start then kill it. check any process with extension ( .scr ). That is my suggestion

    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Monday, May 25, 2009 10:14 PM
  • I need to stop it from coming on, once it's on it's too late.
    Tuesday, May 26, 2009 1:34 PM
  • Hi,

    You can utilize the SystemParametersInfo API provided by the user32.dll, here is an article talking about this: http://www.codeproject.com/KB/cs/ScreenSaverControl.aspx

    Thanks.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
    Wednesday, May 27, 2009 6:39 AM
  • Just tried the code on that page and one of the suggestions, no luck. The problem is that the screensaver is set by the server, so none of these things seem to work with the security policy. I'm also on Vista, so some things don't work because of that also. Here's what I tried:

    Global Hooks (won't work on vista)
    SendKeys every 45 seconds (won't work with security policy)
    http://www.codeproject.com/KB/cs/ScreenSaverControl.aspx sending ActivateScreensaver every 45 seconds (won't work with vista/security policy)
    SetThreadExecutionState(ES_DISPLAY_REQUIRED | ES_CONTINUOUS) (won't work with vista/security policy)

    I feel I may be screwed here, anyone have any other suggestions?
    Wednesday, May 27, 2009 3:40 PM
  • Unfortunately, the computers at my office have the screensaver set by the server for security...
    You've already answered your own question. The screensaver is set to activate for reasons of security. Presumably to force the user to log off when idle. Any attempt to circumvent this is an attempt to bypass security and should be treated as a hostile action.

    Your only option there is to get IT to change policy (is the policy "correct" for all users?).

    If your application needs to run when the user is not logged in, it could run as a system-level service. That will also require IT involvement (approval).
    Wednesday, May 27, 2009 6:01 PM
  • Well I know I shouldn't be doing this, but I still need to. This is an application which connects to the hardware we produce and most likely the computer will have it running as the only application. Most of the customers we sell to will probably have strict security policies and won't be able to change them. I'm trying to debug the problem now but I have to wait 15 minutes every time for the screensaver to come on, so if anybody has any more suggestions I'm willing to try em.

    Wednesday, May 27, 2009 8:38 PM
  • I think what you need to do is re-structure your application so that it doesn't rely on the screensaver on/off to be useful.

    Maybe what you need is a system-level service that can provide communication to the device (and perform whatever unattended activities it has to on the device). Your user-level application can now interface with the service. It no longer cares about screensavers because it has nothing to do until the user interacts with it.
    Wednesday, May 27, 2009 8:42 PM
  • The application is constantly in communication with the hardware, providing a realtime display for the user. The problem is that the screensaver turning on freezes the application for some reason, and only on certain computers. Right now I'm not able to reproduce it on my computer, but it seems to happen on XP computers with the security policy set. I have absolutely no clue why it freezes and right now I have no computers that are xp, have the security policy, have visual studio and can interact with the hardware. Thanks for the advice though, I am looking for alternative ideas to fix this, but it won't help in this case.
    Wednesday, May 27, 2009 9:04 PM
  • Ah, so at last we find the root of the problem. It is not the screen saver itself, but the fact that the connection between the application and the device dies in certain situations. Clearly the solution is not to spam the OS with simulated keyboard pressing, or even to disable the screen saver.

    My guess is that it has nothing to do with the screen saver, but rather is either a result of the computer going to sleep or the workstation being locked, more likely the latter. For that, you can listen for changes. Let me whip up a quick sample (never tried this in a managed app)...

    Oh and as for reproducing the problem, my best advice is to download Virtual PC, install XP and Server X, and now (hopefully) you can simulate the same situation.
    • Proposed as answer by Figo Fei Friday, May 29, 2009 8:00 AM
    • Marked as answer by Figo Fei Monday, June 01, 2009 3:41 AM
    Wednesday, May 27, 2009 9:27 PM
  • Here's a sample app that detects session lock:

    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    public class Form1 : Form
    {
        static void Main()
        {
            Application.Run(new Form1());
        }
    
        [DllImport("wtsapi32")]
        private static extern bool WTSRegisterSessionNotification(HandleRef hwnd, WTSFlag flag);
        [DllImport("wtsapi32")]
        private static extern bool WTSUnRegisterSessionNotification(HandleRef hwnd);
    
        private enum WTSFlag : uint
        {
            NOTIFY_FOR_THIS_SESSION = 0,
            NOTIFY_FOR_ALL_SESSIONS = 1
        }
    
        private enum WTSStatusCode
        {
            WTS_CONSOLE_CONNECT = 1,
            WTS_CONSOLE_DISCONNECT = 2,
            WTS_REMOTE_CONNECT = 3,
            WTS_REMOTE_DISCONNECT = 4,
            WTS_SESSION_LOGON = 5,
            WTS_SESSION_LOGOFF = 6,
            WTS_SESSION_LOCK = 7,
            WTS_SESSION_UNLOCK = 8,
            WTS_SESSION_REMOTE_CONTROL = 9,
        }
    
        private ListBox listBox;
        public Form1()
        {
            listBox = new ListBox();
            listBox.Dock = DockStyle.Fill;
            Controls.Add(listBox);
        }
    
        bool wtsRegistered = false;
    
        protected override void OnHandleCreated(EventArgs e)
        {
            wtsRegistered = WTSRegisterSessionNotification(new HandleRef(this, Handle), WTSFlag.NOTIFY_FOR_ALL_SESSIONS);
            Text = String.Format("WTS Registered : {0}", wtsRegistered);
            base.OnHandleCreated(e);
        }
    
        protected override void OnHandleDestroyed(EventArgs e)
        {
            if (wtsRegistered)
                WTSUnRegisterSessionNotification(new HandleRef(this, Handle));
            base.OnHandleDestroyed(e);
        }
    
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x02B1) // WM_WTSSESSION_CHANGE
            {
                int statusCode = (int)m.WParam;
                if (Enum.IsDefined(typeof(WTSStatusCode), statusCode))
                {
                    switch ((WTSStatusCode)statusCode)
                    {
                        case WTSStatusCode.WTS_SESSION_LOCK:
                            listBox.Items.Add("Session Lock detected.");
                            break;
                        case WTSStatusCode.WTS_SESSION_UNLOCK:
                            listBox.Items.Add("Session Unlock detected.");
                            break;
                    }
                }
                return;
            }
            base.WndProc(ref m);
        }
    }
    
    • Edited by Tergiver Wednesday, May 27, 2009 9:53 PM Added session unlock
    Wednesday, May 27, 2009 9:43 PM
  • I modified the sample above to show both LOCK and UNLOCK detection, as you'll want to shutdown communications during LOCK and resume them (reopen the connection) during UNLOCK.
    Wednesday, May 27, 2009 9:54 PM
  • I decided that this session notification feature was a perfect candidate for a Componant, so here it is:

    using System;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Collections.Generic;
    
    public class SessionNotificationEventArgs : EventArgs
    {
        private uint sessionID;
        private bool remoteControlled;
    
        public uint SessionID
        {
            get { return sessionID; }
        }
    
        public bool RemoteControlled // only valid for SessionRemoteControl event
        {
            get { return remoteControlled; }
        }
    
        public SessionNotificationEventArgs(uint sessionID)
        {
            this.sessionID = sessionID;
        }
    
        public SessionNotificationEventArgs(uint sessionID, bool remoteControlled)
        {
            this.sessionID = sessionID;
            this.remoteControlled = remoteControlled;
        }
    }
    
    public delegate void SessionNotificationEventHandler(object sender, SessionNotificationEventArgs e);
    
    public class SessionNotifier : Component
    {
        private GCHandle thisRoot;
        private object syncObject = new object();
        private bool enabled = false;
        private SessionNotifierNativeWindow nativeWindow;
        private Dictionary<string, Delegate> eventTable;
    
        public SessionNotifier()
        {
            eventTable = new Dictionary<string, Delegate>();
            eventTable.Add("ConsoleConnect", null);
            eventTable.Add("ConsoleDisconnect", null);
            eventTable.Add("RemoteConnect", null);
            eventTable.Add("RemoteDisconnect", null);
            eventTable.Add("SessionLogon", null);
            eventTable.Add("SessionLogoff", null);
            eventTable.Add("SessionLock", null);
            eventTable.Add("SessionUnlock", null);
            eventTable.Add("SessionRemoteControl", null);
        }
    
        public SessionNotifier(IContainer container) : this()
        {
            container.Add(this);
        }
    
        protected override void Dispose(bool disposing)
        {
            if (nativeWindow != null)
                nativeWindow.Stop();
            Enabled = false;
            nativeWindow = null;
            base.Dispose(disposing);
        }
    
        [DefaultValue(false)]
        public bool Enabled
        {
            get
            {
                if (nativeWindow != null)
                    return nativeWindow.Enabled;
                return enabled;
            }
            set
            {
                lock (syncObject)
                {
                    if (enabled != value)
                    {
                        enabled = value;
                        if (!DesignMode)
                        {
                            if (value)
                            {
                                if (nativeWindow == null)
                                    nativeWindow = new SessionNotifierNativeWindow(this);
                                thisRoot = GCHandle.Alloc(this);
                                enabled = nativeWindow.Start();
                            }
                            else
                            {
                                if (nativeWindow != null)
                                    nativeWindow.Stop();
                                if (thisRoot.IsAllocated)
                                    thisRoot.Free();
                            }
                        }
                    }
                }
            }
        }
    
        public event SessionNotificationEventHandler ConsoleConnect
        {
            add { lock (eventTable) { eventTable["ConsoleConnect"] = (SessionNotificationEventHandler)eventTable["ConsoleConnect"] + value; } }
            remove { lock (eventTable) { eventTable["ConsoleConnect"] = (SessionNotificationEventHandler)eventTable["ConsoleConnect"] - value; } }
        }
        public event SessionNotificationEventHandler ConsoleDisconnect
        {
            add { lock (eventTable) { eventTable["ConsoleDisconnect"] = (SessionNotificationEventHandler)eventTable["ConsoleDisconnect"] + value; } }
            remove { lock (eventTable) { eventTable["ConsoleDisconnect"] = (SessionNotificationEventHandler)eventTable["ConsoleDisconnect"] - value; } }
        }
        public event SessionNotificationEventHandler RemoteConnect
        {
            add { lock (eventTable) { eventTable["RemoteConnect"] = (SessionNotificationEventHandler)eventTable["RemoteConnect"] + value; } }
            remove { lock (eventTable) { eventTable["RemoteConnect"] = (SessionNotificationEventHandler)eventTable["RemoteConnect"] - value; } }
        }
        public event SessionNotificationEventHandler RemoteDisconnect
        {
            add { lock (eventTable) { eventTable["RemoteDisconnect"] = (SessionNotificationEventHandler)eventTable["RemoteDisconnect"] + value; } }
            remove { lock (eventTable) { eventTable["RemoteDisconnect"] = (SessionNotificationEventHandler)eventTable["RemoteDisconnect"] - value; } }
        }
        public event SessionNotificationEventHandler SessionLogon
        {
            add { lock (eventTable) { eventTable["SessionLogon"] = (SessionNotificationEventHandler)eventTable["SessionLogon"] + value; } }
            remove { lock (eventTable) { eventTable["SessionLogon"] = (SessionNotificationEventHandler)eventTable["SessionLogon"] - value; } }
        }
        public event SessionNotificationEventHandler SessionLogoff
        {
            add { lock (eventTable) { eventTable["SessionLogoff"] = (SessionNotificationEventHandler)eventTable["SessionLogoff"] + value; } }
            remove { lock (eventTable) { eventTable["SessionLogoff"] = (SessionNotificationEventHandler)eventTable["SessionLogoff"] - value; } }
        }
        public event SessionNotificationEventHandler SessionLock
        {
            add { lock (eventTable) { eventTable["SessionLock"] = (SessionNotificationEventHandler)eventTable["SessionLock"] + value; } }
            remove { lock (eventTable) { eventTable["SessionLock"] = (SessionNotificationEventHandler)eventTable["SessionLock"] - value; } }
        }
        public event SessionNotificationEventHandler SessionUnlock
        {
            add { lock (eventTable) { eventTable["SessionUnlock"] = (SessionNotificationEventHandler)eventTable["SessionUnlock"] + value; } }
            remove { lock (eventTable) { eventTable["SessionUnlock"] = (SessionNotificationEventHandler)eventTable["SessionUnlock"] - value; } }
        }
        public event SessionNotificationEventHandler SessionRemoteControl
        {
            add { lock (eventTable) { eventTable["SessionRemoteControl"] = (SessionNotificationEventHandler)eventTable["SessionRemoteControl"] + value; } }
            remove { lock (eventTable) { eventTable["SessionRemoteControl"] = (SessionNotificationEventHandler)eventTable["SessionRemoteControl"] - value; } }
        }
    
        private void FireEvent(string eventName, SessionNotificationEventArgs e)
        {
            SessionNotificationEventHandler eventHandler;
            if ((eventHandler = (SessionNotificationEventHandler)eventTable[eventName]) != null)
                eventHandler(this, e);
        }
    
        private sealed class SessionNotifierNativeWindow : NativeWindow
        {
            private bool wtsRegistered = false;
            private SessionNotifier owner;
    
            internal SessionNotifierNativeWindow(SessionNotifier owner)
            {
                this.owner = owner;
            }
            
            ~SessionNotifierNativeWindow()
            {
                Stop();
            }
    
            public bool Enabled
            {
                get { return wtsRegistered && Handle != IntPtr.Zero; }
            }
    
            private bool EnsureHandle()
            {
                if (Handle == IntPtr.Zero)
                {
                    CreateParams cp = new CreateParams();
                    cp.Style = 0;
                    cp.ExStyle = 0;
                    cp.ClassStyle = 0;
                    cp.Caption = GetType().Name;
                    if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                        cp.Parent = (IntPtr) (-3); // MESSAGE_WINDOW
                    CreateHandle(cp);
                }
                return Handle != IntPtr.Zero;
            }
    
            [DllImport("wtsapi32")]
            private static extern bool WTSRegisterSessionNotification(HandleRef hwnd, int flag);
            [DllImport("wtsapi32")]
            private static extern bool WTSUnRegisterSessionNotification(HandleRef hwnd);
            [DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
            public static extern int GetCurrentThreadId();
            [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
            public static extern int GetWindowThreadProcessId(HandleRef hWnd, out int lpdwProcessId);
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern IntPtr PostMessage(HandleRef hwnd, int msg, int wparam, int lparam);
            [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
            public static extern bool IsWindow(HandleRef hWnd);
            [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
            [return: MarshalAs(UnmanagedType.Bool)] // not the correct signature, but works for our only request (SM_REMOTECONTROL)
            private static extern bool GetSystemMetrics(int nIndex);
    
            public override void DestroyHandle()
            {
                Stop(false, IntPtr.Zero);
                base.DestroyHandle();
            }
    
            public override void ReleaseHandle()
            {
                Stop(false, IntPtr.Zero);
                base.ReleaseHandle();
            }
    
            protected override void OnThreadException(Exception e)
            {
                Application.OnThreadException(e);
            }
    
            public bool Start()
            {
                if (!wtsRegistered && EnsureHandle())
                    wtsRegistered = WTSRegisterSessionNotification(new HandleRef(this, Handle), 1); // NOTIFY_FOR_ALL_SESSIONS
                return wtsRegistered;
            }
    
            public void Stop()
            {
                Stop(true, IntPtr.Zero);
            }
    
            private void Stop(bool destroyHwnd, IntPtr hWnd)
            {
                if (hWnd == IntPtr.Zero)
                    hWnd = Handle;
    
                if (GetInvokeRequired(hWnd))
                    PostMessage(new HandleRef(this, hWnd), 0x10, 0, 0); // WM_CLOSE
                else
                {
                    lock (this)
                    {
                        if (hWnd != IntPtr.Zero && IsWindow(new HandleRef(this, hWnd)))
                        {
                            if (wtsRegistered)
                                WTSUnRegisterSessionNotification(new HandleRef(this, Handle));
                            if (destroyHwnd)
                                DestroyHandle();
                        }
                    }
                }
            }
    
            private bool GetInvokeRequired(IntPtr hWnd)
            {
                if (hWnd != IntPtr.Zero)
                {
                    int num;
                    int windowThreadProcessId = GetWindowThreadProcessId(new HandleRef(this, hWnd), out num);
                    int currentThreadId = GetCurrentThreadId();
                    return (windowThreadProcessId != currentThreadId);
                }
                return false;
            }
    
            protected override void WndProc(ref Message m)
            {
                if (m.Msg == 0x02B1) // WM_WTSSESSION_CHANGE
                {
                    switch ((int)m.WParam)
                    {
                        case 1: // WTS_CONSOLE_CONNECT
                            owner.FireEvent("ConsoleConnect", new SessionNotificationEventArgs((uint)m.LParam));
                            break;
                        case 2: // WTS_CONSOLE_DISCONNECT
                            owner.FireEvent("ConsoleDisconnect", new SessionNotificationEventArgs((uint)m.LParam));
                            break;
                        case 3: // WTS_REMOTE_CONNECT
                            owner.FireEvent("RemoteConnect", new SessionNotificationEventArgs((uint)m.LParam));
                            break;
                        case 4: // WTS_REMOTE_DISCONNECT
                            owner.FireEvent("RemoteDisconnect", new SessionNotificationEventArgs((uint)m.LParam));
                            break;
                        case 5: // WTS_SESSION_LOGON
                            owner.FireEvent("SessionLogon", new SessionNotificationEventArgs((uint)m.LParam));
                            break;
                        case 6: // WTS_SESSION_LOGOFF
                            owner.FireEvent("SessionLogoff", new SessionNotificationEventArgs((uint)m.LParam));
                            break;
                        case 7: // WTS_SESSION_LOCK
                            owner.FireEvent("SessionLock", new SessionNotificationEventArgs((uint)m.LParam));
                            break;
                        case 8: // WTS_SESSION_UNLOCK
                            owner.FireEvent("SessionUnlock", new SessionNotificationEventArgs((uint)m.LParam));
                            break;
                        case 9: // WTS_SESSION_REMOTE_CONTROL
                            owner.FireEvent("SessionRemoteControl", new SessionNotificationEventArgs((uint)m.LParam, GetSystemMetrics(0x2001))); // SM_REMOTECONTROL
                            break;
                    }
                    return;
                }
                else if (m.Msg == 0x10) // WM_CLOSE
                {
                    Stop(true, m.HWnd);
                    return;
                }
                
                base.WndProc(ref m);
            }
        }
    }
    
    • Edited by Tergiver Thursday, May 28, 2009 12:18 AM Added RemoteControlled to event args for SessionRemoteControl event
    Wednesday, May 27, 2009 11:17 PM
  • I attempted to add power suspend/resume to that componant, but it seems that message windows don't get the message. So going back to the first sample, the following should be all you need to suspend and resume communications with your device:

    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    public class Form1 : Form
    {
        static void Main()
        {
            Application.Run(new Form1());
        }
    
        [DllImport("wtsapi32")]
        private static extern bool WTSRegisterSessionNotification(HandleRef hwnd, int flag);
        [DllImport("wtsapi32")]
        private static extern bool WTSUnRegisterSessionNotification(HandleRef hwnd);
    
        const int NOTIFY_FOR_THIS_SESSION = 0;
        const int NOTIFY_FOR_ALL_SESSIONS = 1;
    
        private ListBox listBox;
        public Form1()
        {
            listBox = new ListBox();
            listBox.Dock = DockStyle.Fill;
            Controls.Add(listBox);
        }
    
        bool wtsRegistered = false;
    
        protected override void OnHandleCreated(EventArgs e)
        {
            wtsRegistered = WTSRegisterSessionNotification(new HandleRef(this, Handle), NOTIFY_FOR_THIS_SESSION);
            Text = String.Format("WTS Registered : {0}", wtsRegistered);
            base.OnHandleCreated(e);
        }
    
        protected override void OnHandleDestroyed(EventArgs e)
        {
            if (wtsRegistered)
                WTSUnRegisterSessionNotification(new HandleRef(this, Handle));
            base.OnHandleDestroyed(e);
        }
    
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x02B1) // WM_WTSSESSION_CHANGE
            {
                switch ((int)m.WParam)
                {
                    case 7: // WTS_SESSION_LOCK:
                        listBox.Items.Add("Session locked.");
                        break;
                    case 8: // WTS_SESSION_UNLOCK:
                        listBox.Items.Add("Session Unlocked.");
                        break;
                }
                return;
            }
            else if (m.Msg == 0x0218) // WM_POWERBROADCAST
            {
                switch ((int)m.WParam)
                {
                    case 0x04: // PBT_APMSUSPEND
                        listBox.Items.Add("Power suspended.");
                        break;
                    case 0x12: // PBT_APMRESUMEAUTOMATIC
                        listBox.Items.Add("Power resumed.");
                        break;
                }
            }
            base.WndProc(ref m);
        }
    }
    
    • Proposed as answer by Figo Fei Friday, May 29, 2009 7:41 AM
    • Marked as answer by ScottyDoesKnow Monday, June 15, 2009 7:46 PM
    Thursday, May 28, 2009 2:56 PM
  • If you have a optical mouse, turn it upside down. It usually jumps around the screen every 20-30 seconds and stops the screen saver from coming on. I do this at uni so the 'log this computer off' screen doesn't pop up if I run to grab a coffee or whatever.
    • Proposed as answer by zaTTaz Monday, June 15, 2009 3:53 AM
    • Unproposed as answer by ScottyDoesKnow Monday, June 15, 2009 1:51 PM
    • Proposed as answer by zaTTaz Monday, June 15, 2009 8:29 PM
    • Unproposed as answer by ScottyDoesKnow Monday, June 15, 2009 9:11 PM
    Monday, June 15, 2009 3:53 AM
  • If you have a optical mouse, turn it upside down. It usually jumps around the screen every 20-30 seconds and stops the screen saver from coming on. I do this at uni so the 'log this computer off' screen doesn't pop up if I run to grab a coffee or whatever.

    Would you call this "Social Engineering", or an "Apathetic Security Hole"?
    Monday, June 15, 2009 7:14 PM
  • Considering he shouldn't be messing around with any settings/code on his company computer, unless IT is in on it, I'd call something like this the only option!
    If anything goes wrong with the system people like this are the first to get into hot water at work, even if its a completely unrelated problem...
    Monday, June 15, 2009 8:39 PM
  • Except I will have to deliver this application to professionals, and I feel I may get laughed at if I tell them to flip their mouse over every time they're not interacting with it.

    Monday, June 15, 2009 9:11 PM
  • Did you ever solve the problem Scotty?
    Monday, June 15, 2009 9:12 PM
  • I haven't had any time to recreate it. Have too much on my plate so the easiest way is to tell them that the security settings on the screensaver could mess it up. Plus if they're doing certain things with the application which take a while with no user input, even disconnecting for the screensaver isn't going to be clean. I have this favourited though for when I get around to it. Plus I haven't suggested the mouse inversion solution yet, maybe he'll be all for it.
    Monday, June 15, 2009 9:17 PM
  • Highly likely. They may even complain about the costs associated with upgrading to an optical unit if they are traditional ball unit fans also. Money is tight these days! I thought it was just the link between your computer and theirs that was the problem sorry so thought I'd throw it out there as a simple fix. Good luck!
    Monday, June 15, 2009 9:43 PM
  • I was curious to know if disconnecting and reconnecting for the events I mentioned solved it or not, let me know if you get around to it.
    Monday, June 15, 2009 9:43 PM
  • This may not be your exact problem, but I finally figured out why VirtualPC / XP Mode intermittantly stops responding to the keyboard (and thus to sendkeys) after my Host desktop is locked.

    It is indeed a wierd XP Mode bug:

    If you click on a window inside the VirtualPC’s desktop before doing a Ctrl-Alt-Del to lock the host computer, the virtualPC's keyboard driver stops responding (even after you unlock the host screen, the mouse works but not the keyboard – you can’t even type manually into notepad on the VirtualPC desktop). Unlike your problem, my mouse keeps working, however.

     

    Coincidentally, VirtualPC's keyboard driver also stops responding to SendKeys sent from within my program running on VirtualPC.  It stays in that state until you minimize, maximize, or resize the VirtualPC desktop. Just clicking on something in the VirtualPC desktop will not restore keyboard function. Neither will clicking back and forth from Host desktop to the VirtualPC desktop. The whole VirtualPC desktop has to be resized or minimized/maximized, or else you have to click on the "Windows XP Mode" button on the Host PC's taskbar.

     

    To prevent it from doing that, all I wound up having to do is click on a window on the host computer’s desktop before doing the CTRL-Alt-Delete to lock the host screen. The Virtual PC keyboard driver then keeps working (and thus SendKeys keeps working), even while the host screen is locked and also after it’s unlocked.

     

    Maybe your mouse problem is a different version of the same sort of bug.

     

    • Proposed as answer by TxCharlie Saturday, October 22, 2011 3:45 AM
    • Edited by TxCharlie Saturday, October 22, 2011 3:50 AM
    Saturday, October 22, 2011 3:44 AM
  • I know this thread is old but this may help others:

    http://www.codeproject.com/Articles/17067/Controlling-The-Screen-Saver-With-C

    "To facilitate testing the KillScreenSaver( ) method, the test program uses a periodic timer. The timeout period can be set and the timer can be toggled using the Start/Stop Timer button. If the period is greater than the screen saver timeout, a call to KillScreenSaver( ) will terminate the screen saver. If the period is less, the screen saver timeout is reset by invoking SetScreenSaverActive( TRUE ), preventing the screen saver from running until after a full timeout has expired."

    Wednesday, January 09, 2013 8:37 PM
  • Heh, when I am at work waiting for a long process to conclude and I do not want the scereen saver coming on, I put the optical mouse in my shirt pocket.  Thus everytime I breath a breath of fresh air, the mouse moves slightly preventing the screen saver from coming on!  Thus another usless solution to the problem.  Heh, but it works LOL.

    MattMo

    Tuesday, June 03, 2014 1:32 PM