Burgi
Burgi

Reputation: 434

Suspending a PC

I'm fairly new to C#, I am trying to write my own UWP app to suspend the PC after a user entered interval.

I have managed to get my timer working but I am struggling to understand how to call the method to suspend the computer. I tried using the code suggested here

Application.SetSuspendState(PowerState.Suspend, true, true);

But I get the following error when I try to build the project:

CS0117 'Application' does not contain a definition for 'SetSuspendState' SystemTimer C:\Users\xxxxx\Source\Workspaces\Workspace\SystemTimer\SystemTimer\MainPage.xaml.cs 52

I found an MSDN entry on PowerState which suggest I use the System.Windows.Form library. I tried adding it to the namespaces but get an error saying that Form is not an available namespace in System.Windows.

I feel I am probably missing something fundamental but I am not entirely sure what it is.

Upvotes: 1

Views: 920

Answers (2)

Dirk Vollmar
Dirk Vollmar

Reputation: 176159

I feel I am probably missing something fundamental but I am not entirely what it is.

The world of (UWP) apps is a different world. An app is not supposed to interfere (i.e. to mess around) with its underlying system. You basically run in a sandboxed environment and you only get what is provided by the API. Shutting down or suspending the system is not part of this API.

What options do you have? You probably are better off if you create a desktop app. Then you will be able to use all the available desktop APIs including System.Windows.Form.

Upvotes: 2

developer__c
developer__c

Reputation: 636

Have you tried implementing this using Interop Services?

[DllImport("Powrprof.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool SetSuspendState(bool hiberate, bool forceCritical, bool disableWakeEvent);

// Hibernate
SetSuspendState(true, true, true);
// Standby
SetSuspendState(false, true, true);

Upvotes: -1

Related Questions