Winforms application stops working if computer goes into hibernation

Asked

Viewed 63 times

3

I have an application in winforms, which is on the company server, so I pull the shortcut to the desktop and the collaborator runs normally.

The problem started to happen, when we exchanged some computers (Desktop) for notebook’s, when the notebook goes into hibernation, when returning the application stops working.

At the moment, I make a palliative solution, I detect when windows is to go into hibernation and finish the application, but it is not quite certain..

Would anyone have any other choice? or could you give me some hint?

  • app.exe version 1 running, notebook hibernates, you update the application on the server (version 2), vc "wake up" the notebook with the application already open. Which version should work ? When hibernating loses connection, when coming back no longer exists session / connection to the server, will stop even. I prefer to keep the executables on the client computer...

  • @Rovannlinhalis version is the same, not updated.. I can keep the version on the computer, however, I will need to elaborate, a process to check the version, if it is outdated, I would need to assemble an update process.. I have some methods that save files, would need to redo them to save the file in a network path.. and this I want to avoid..

  • The version example was just to demonstrate what can happen... the computer hibernates, you can delete the file from the server, when it comes back has no way to work even. Disabling hibernation is an option (great, in my view it does no good, computer gets slow afterwards, etc.) and if you need to update, check out this answer: https://answall.com/a/277495/69359

1 answer

1

You can make your application not allow windows to hibernate while open. Something 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;
}
  • Good idea, I will try this solution and warning about. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.