How do I cancel standby and hibernation?

my application running in c # using third party framework.it works as a user interface for many application.my questions when my application is running and the system should not be doing fallback / hybernate OS operations. somehow i have to cancel event for wait / hybernate raised by OS.please, somebody helped me in this regard.

Thanks Sun

+1


source to share


3 answers


This post explains how to prevent computer migration using SetThreadExecutionState . The code looks like this:

public partial class Window1 : Window
{
    private uint m_previousExecutionState;

    public Window1()
    {
        InitializeComponent();

        // Set new state to prevent system sleep (note: still allows screen saver)
        m_previousExecutionState = NativeMethods.SetThreadExecutionState(
            NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED);
        if (0 == m_previousExecutionState)
        {
            MessageBox.Show("Call to SetThreadExecutionState failed unexpectedly.",
                Title, MessageBoxButton.OK, MessageBoxImage.Error);
            // No way to recover; fail gracefully
            Close();
        }
    }

    protected override void OnClosed(System.EventArgs e)
    {
        base.OnClosed(e);

        // Restore previous state
        if (0 == NativeMethods.SetThreadExecutionState(m_previousExecutionState))
        {
            // No way to recover; already exiting
        }
    }
}

internal static class NativeMethods
{
    // Import SetThreadExecutionState Win32 API and necessary flags
    [DllImport("kernel32.dll")]
    public static extern uint SetThreadExecutionState(uint esFlags);
    public const uint ES_CONTINUOUS = 0x80000000;
    public const uint ES_SYSTEM_REQUIRED = 0x00000001;
}

      



If you like the app described in the post there is an updated version here .

+4


source


Unable to cancel event from .NET. You will need to do this with P / Invoke and Win32 API by accessing WM_POWERBROADCAST

and returning BROADCAST_QUERY_DENY

. Check out this CodeGuru page for stepping in the right direction.



Also interesting is this page , which details some of the events that are triggered in .NET when a user logs out or suspends / hibernates.

+1


source


Powercfg and shutdown (especially shutdown /a

to abort the system shutdown) might be useful, perhaps.

0


source







All Articles