tags:

views:

512

answers:

3

my application running in C#, using third party framework.it works as an UI for many application.my question is when my appplication is running and the system should not happen standby/hybernate OS operations. somehow i have to cancel the event for standby/hybernate raised by OS.please any one help my in this regards.

Thanks Sun

A: 

There's no way to cancel the event from within .NET. You'll have to do it with P/Invoke and the Win32 API by handling WM_POWERBROADCAST and returning BROADCAST_QUERY_DENY. Take a look at this CodeGuru page for a push in the right direction.

Also of interest would be this page, detailing some events that are fired in .NET when a user logs off or suspend/hibernates.

lc
hi lci user the follwing codeprotected override void WndProc(ref System.Windows.Forms.Message m)....m.Result = new IntPtr((int)WindowMessage.BROADCAST_QUERY_DENY);....returnThis is working when i use the code in secondary window(in my app secondary are windows which are C# windows)the primary windows are created my using the data got from backend application.i have to use this method in orimary window part.i have to use in a file where 'wndproc' method is not alllowed.any ideathanks Sun
I'm not sure I 100% follow you with primary and secondary windows, but any "window" (Windows.Forms.Form) should have a message loop and therefore a WndProc callback. Put the code in whatever form you have Application.Run()'ed.All else fails, you can make a little hidden form that sits there and listens for this message.
lc
Hi thanks for ur replyi will put it shortthe windows will be drawn inside the framework means the framework will launch first we have no control on the framework.the framework has menu's ,contains the application name click on anyparicular menu corresponding app will sent data to draw the windows say sents all the details necassary to draw the windows.now our job begin, parse the details and draw the window inside the framework.
so here when the app is launched and quite for sometime the standby happens, we want just to cancel the event standby now i had created an event SystemEvents.PowermodeChanged += PowerModeChangedEventHandler(SystemEvents_PowerModdeChanged);is this work? wat should be written inside the Event SystemEvents_PowerModdeChanged()to cancel the standby ?Please help
+1  A: 

This blog post describes how to prevent a PC from going to sleep 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 application described in the post, there is an updated version here.

John Myczek
A: 

Powercfg and shutdown (especially shutdown /a to abort a system shutdown) might prove useful, possibly.

Kevin L.