private void CheckPowerLineStatus() { //checks if power has been lost or restored PowerLineStatus currPowerLineStatus = SystemInformation.PowerStatus.PowerLineStatus; if (prevPowerLineStatus == PowerLineStatus.Online) { if (currPowerLineStatus == PowerLineStatus.Offline || currPowerLineStatus == PowerLineStatus.Unknown) { if (!pNotif) { pNotif = true; switch (cbShutdownAction.SelectedIndex) { case 0: int action = SelectionInputDialog.ShowSelectionInputDialog(this, "Choose Shutdown Action", ShutdownManager.GetShutdownActions(SHUTDOWN_TYPE.SHUTDOWN_POWER_LOSS)); //make sure we have a valid shutdown action if (action >= 0) { ShutdownManager.Shutdown((SHUTDOWN_ACTION_POWER_LOSS)action); } break; default: SHUTDOWN_ACTION_POWER_LOSS sdAction = (SHUTDOWN_ACTION_POWER_LOSS)cbShutdownAction.SelectedIndex; ShutdownManager.Shutdown(sdAction); //MessageBox.Show(this, "Computer Shutdown Mode: " + sdAction.ToString(), "Warning"); break; } //notifications come as dialogs which will block the //execution of the statements after it //execution continues when the dialog is closed, //when this happens, notification is closed pNotif = false; } } } else { if (currPowerLineStatus == PowerLineStatus.Online) { if (pNotif) { SelectionInputDialog.CloseDialog(); pNotif = false; } } } prevPowerLineStatus = currPowerLineStatus; }
public static void Shutdown(SHUTDOWN_ACTION_POWER_LOSS action) { switch (action) { case SHUTDOWN_ACTION_POWER_LOSS.SLEEP: Application.SetSuspendState(PowerState.Suspend, true, false); break; case SHUTDOWN_ACTION_POWER_LOSS.HIBERNATE: Application.SetSuspendState(PowerState.Hibernate, true, false); break; case SHUTDOWN_ACTION_POWER_LOSS.SHUTDOWN: Process.Start(@"..\cmd_shutdown\shutdown.exe"); break; } }