Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. public partial class Window1 : Window
  2. {
  3. private uint m_previousExecutionState;
  4.  
  5. public Window1()
  6. {
  7. InitializeComponent();
  8.  
  9. // Set new state to prevent system sleep (note: still allows screen saver)
  10. m_previousExecutionState = NativeMethods.SetThreadExecutionState(
  11. NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED);
  12. if (0 == m_previousExecutionState)
  13. {
  14. MessageBox.Show("Call to SetThreadExecutionState failed unexpectedly.",
  15. Title, MessageBoxButton.OK, MessageBoxImage.Error);
  16. // No way to recover; fail gracefully
  17. Close();
  18. }
  19. }
  20.  
  21. protected override void OnClosed(System.EventArgs e)
  22. {
  23. base.OnClosed(e);
  24.  
  25. // Restore previous state
  26. if (0 == NativeMethods.SetThreadExecutionState(m_previousExecutionState))
  27. {
  28. // No way to recover; already exiting
  29. }
  30. }
  31.  
  32. private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
  33. {
  34. // Start an instance of the NavigateUri (in a browser window)
  35. Process.Start(((Hyperlink)sender).NavigateUri.ToString());
  36. }
  37. }
  38.  
  39. internal static class NativeMethods
  40. {
  41. // Import SetThreadExecutionState Win32 API and necessary flags
  42. [DllImport("kernel32.dll")]
  43. public static extern uint SetThreadExecutionState(uint esFlags);
  44. public const uint ES_CONTINUOUS = 0x80000000;
  45. public const uint ES_SYSTEM_REQUIRED = 0x00000001;
  46. }
  47.  
  48. Windows Registry Editor Version 5.00
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement