Shutdown Windows After Simulation

Shutdown Windows after simulation

Yes, look at the function shutdown in the package fun.

The flags for system command shutdown depends on your operating system, the function simply calls the appropriately flagged command.

fun::shutdown

function (wait = 0)
{
Sys.sleep(wait)
ifelse(.Platform$OS.type == "windows", shell("shutdown -s -t 0"),
system("shutdown -h now"))
}

How to simulate Windows shutdown for debugging?

There is a tool named Restart Manager (rmtool.exe) in the Microsoft's Logo Testing Tools for Windows, which can be used to send shutdown and restart messages to a process. Logo testing tools can be downloaded here:

http://download.microsoft.com/download/d/2/5/d2522ce4-a441-459d-8302-be8f3321823c/LogoToolsv1.0.msi

Then you can simulate shutdown for your process:

rmtool.exe -p [PID] -S

where [PID] is the process ID. According to the Vista Logo Certification Test Cases document,

Restart Manager shutdown messages are:

a. WM_QUERYENDSESSION with LPARAM = ENDSESSION_CLOSEAPP(0x1): GUI applications must respond (TRUE) immediately to prepare for a restart.

b. WM_ENDSESSION with LPARAM = ENDSESSION_CLOSEAPP(0x1): The application must shutdown within 5 seconds (20 seconds for services).

c. CTRL_SHUTDOWN_EVENT: Console applications must shutdown immediately.

Is there a way to shut down the computer from inside R

You can try running a system command like this (for windows only):

system("shutdown -f -t 1")

For Mac/Linux try (permissions might not allow this, though):

system("shutdown -h now")

Warning: This command will force close all running processes...

Keep process alive during shutdown

The Server should be keeping track of the Clients that are connected to it. So, if your apps are busy performing housecleaning, they ARE blocking shutdown, even if just momentarily. So what is wrong with letting Windows show a message to the user saying that?

When the Server gets notified of an imminent shutdown, have it call ShutdownBlockReasonCreate() if there are any Clients connected. Regardless of whether the Clients perform housecleaning or not, when the last Client disconnects then the Server can call ShutdownBlockReasonDestroy().

Shut down iPhone in simulator

Yes. Press Command+Q to quit the simulator.

This will close all the apps running in the simulator.

Pressing the home button only returns to the home screen without quitting the apps(ios 4 or later) because of multitasking.

You can also double tap the home button, click and hold on the icons at the bottom of the screen till a red circle appear, and click on the red circle to close the running app.

Shutdown computer, how operating systems achieve it? (C Kernel development)

To sum up all the comments in an answer so it stays:

The modern way (circa 2020) to handle power management and shutdown is to use the Advanced Configuration and Power Interface (ACPI), see for example Getting Started With ACPI and OS Shutdown.

History of ACPI

ACPI was developed through collaboration between Intel, Microsoft,
Toshiba, HP, and Phoenix in the mid-1990s. Before the development of
ACPI, operating systems (OS) primarily used BIOS (Basic Input/Output
System) interfaces for power management and device discovery and
configuration. This power management approach used the OS’s ability to
call the system BIOS natively for power management. The BIOS was also
used to discover system devices and load drivers based on probing
input/output (I/O) and attempting to match the correct driver to the
correct device (plug and play). The location of devices could also be
hard coded within the BIOS because the platform itself was
non-enumerable.These solutions were problematic in three key ways.
First, the behavior of OS applications could be negatively affected by
the BIOS-configured power management settings, causing systems to go
to sleep during presentations or other inconvenient times. Second, the
power management interface was proprietary on each system. This
required developers to learn how to configure power management for
each individual system. Finally, the default settings for various
devices could also conflict with each other, causing devices to crash,
behave erratically, or become undiscoverable.ACPI was developed to
solve these problems and others.

What is ACPI?

ACPI can first be understood as an architecture-independent power
management and configuration framework that forms a subsystem within
the host OS. This framework establishes a hardware register set to
define power states (sleep, hibernate, wake, etc). The hardware
register set can accommodate operations on dedicated hardware and
general purpose hardware.The primary intention of the standard ACPI
framework and the hardware register set is to enable power management
and system configuration without directly calling firmware natively
from the OS. ACPI serves as an interface layer between the system
firmware (BIOS) and the OS, as shown in Figure0-1 and Figure0-2, with
certain restrictions and rules.

ACPI overview

Source: Advanced Configuration and Power Interface (ACPI) Specification, Version 6.3 January 2019

This answer provides the historical background on the problem of OS controlling computer power and shutdown, and why it has taken so long to achieve some standard. Quoting:

Some history...

Before 1995 and the adoption of the ATX standard, the vast majority of
desktop PCs had power switches which were directly connected to the
power supply, and acted as mechanical switches only, interrupting the
electric circuit when opened. It was therefore impossible for software
to control the state of the power supply. But that wasn’t much of a
problem initially: when the IBM PC was designed, storage media
(including hard drives) had no caches, so when the hardware told the
operating system that a write was finished, it really was. Under DOS,
the kernel and shell worked together to ensure that when the DOS
prompt was displayed, all the buffers were flushed; when software
caches appeared, they adhered to this too (at least, the well-behaved
ones did). Users were taught to exit programs, wait for the prompt,
and wait for drive lights to switch off before powering the system
down. (They might also need to PARK the drive heads but that’s another
story.) Even with pre-95 versions Windows, users exited to DOS before
switching the system off.

Windows 95 and other multi-tasking operating systems changed the
picture: they didn’t “exit to DOS” on shutdown (either because they
weren’t supposed to, or because there was no DOS to return to), so
users couldn’t wait for a prompt to appear before switching off. In
most truly multi-tasking systems there’s never really a quiescent
state where the system is safe to power off, in normal operation; so
most multi-tasking operating systems have a way for the user to say “I
want to power the system down, prepare to do so”, and the operating
system then needs to tell the user when it’s safe to power off. This
ensures that all applications have finished writing the user’s files
to disk, and that the system is in a consistent state (ignoring hard
drive caches here...).

Shutting down PCs

Two features brought system power under operating system control: APM
on the one hand, and ATX on the other. APM, which was designed for
laptops initially, provided mechanisms for software to request changes
in the system’s power state: fully on, in standby, suspended, or off.
ATX changed the physical connections in the system so that power
control became possible everywhere: it required that the power button
no longer be a switch directly connected to the power supply, but
instead that it be connected to the motherboard, and that the
motherboard control the power supply itself. The power supply was also
changed so that it would supply a small amount of current all the
time, allowing the system to be left in “soft off” status, i.e. with
enough capabilities to turn itself back on again when requested to do
so.

You can see an example of the use of APM to power off a PC in
Shutdown, a small assembly-language program written for DOS. Operating
systems such as Windows 95 (with the APM drivers installed) would do
the same thing.

It was quite exciting (to me anyway) to see APM and ATX roll out
progressively in the second half of the nineties, and see systems
suddenly acquire the ability to turn themselves off without human
intervention, and to turn themselves back on at the press of a key on
some systems. This was yet another sign of PCs “growing up” (“real”
computers, i.e. Unix workstations in my mind at the time, had had the
ability for a while, as had Macs).

Why did it take so long?

All this doesn’t address the actual question:

Was it really that hard to implement a self shut-off? What is the reason it took quite long for computers to feature this.


If you design it in from the start, it’s not all that hard to
implement self shut-off, and many systems existed with this ability
quite a few years before the PC acquired it.

[..]In the early nineties, surprising as it may seem now, there was a fair
amount of uncertainty as to what the computing future held. Apple was
making its comeback with cheaper Macs, workstation manufacturers were
releasing lower-priced systems (or rather, not-so-expensive systems),
a variety of operating systems and platforms were vying for attention
(Be, RiscPC...), IBM was still pushing OS/2 and Taligent, Microsoft
was pushing Windows NT, etc.

Eventually an alliance of companies took it upon itself to “remedy”
this situation: Intel and Microsoft (referred to at the time as
Wintel). This started in the early nineties, but wasn’t a done deal
for quite a while; when ATX was published in 1995 (by Intel on its
own), pundits liked it but weren’t sure it would convince the
industry, although they were proved wrong fairly quickly. Windows 95
sealed the deal though and Intel and Microsoft became the definers of
the PC platform (with the PC System Design Guide in particular).

Here is a Microsoft patent from the 90's for software-controlled computer hibernation which is related to software-controlled power management.

Effectively, software-controlled shutdown is a kind of simulation of shutdown, as real shutdown takes place when hardware power is actually switched off. Soft shutdown effectively is to make computer do absolutely nothing and consume as less power as possible. So OS makes sure to end all processes and make the CPU go to specific OS routines that simulate a shutdown (note: this state usually cannot be reversed except by a hardware interrupt of restart). Whether the OS routines that make soft-shutdown use ACPI or some other method/interface to simulate is another issue.

This post describes, roughly in outline, the shutdown process of the linux kernel, to get an idea.

[..]Anyways in there we have basically three functions that sketch the
process of shutting down the system

void kernel_halt(void) // which ends with a system in halt state
void kernel_power_off(void) // which ends with a system powered off
void kernel_restart(char *cmd) // which ends the system to yet restart it

Those functions are very brief and can be hence pasted here in
complete. Their code best shows what steps are taken on the way to
shutdown in the kernel. (the comments are by me and might not be 100%
ideal and correct, check yourself for being sure. It is simple a try.

void kernel_halt(void)
{
// 1st step does:
// a) call functions/callback registered to run at reboot/shutdown
// b) set system_sate to SYSTEM_HALT
// c) stop the userspacetool interaction
// d) call device_shutdown() function
kernel_shutdown_prepare(SYSTEM_HALT);

// 2nd step: I think this is mostly a necessity for multi-cpu systems
migrate_to_reboot_cpu();

// 3rd step:
// syscore_shutdown - Execute all the registered system core shutdown callbacks
syscore_shutdown();

// 4th messages
pr_emerg("System halted\n");
kmsg_dump(KMSG_DUMP_HALT);

// 5th call arch specific cpu-halt-code
machine_halt();
}

Windows 10 shutdown delay, VB.NET

Well, one thing you can do is to make your application trigger a shutdown command after finishing the necessary work.

Update:

After investigating this, turns out that the WndProc method with ENDSESSION message gets triggered more than once, causing CleanUpAndSave() to also be carried out again. Hence, in addition to the shutdown command, you'll need to add a boolean to check if the message has already been sent from the system to your application.

The following code was tested and it works just fine on both Windows 7 and windows 10:

Private ShutdownDelayed As Boolean

Protected Overrides Sub WndProc(ByRef aMessage As Message)
Const WM_QUERYENDSESSION As Integer = &H11
Const WM_ENDSESSION As Integer = &H16

If aMessage.Msg = WM_QUERYENDSESSION OrElse aMessage.Msg = WM_ENDSESSION Then
' Check if the message was sent before and the shutdown command is delayed.
If ShutdownDelayed Then Exit Sub

' Block shutdown
ShutdownBlockReasonCreate(Me.Handle, "Testing 123...")
ShutdownDelayed = True

' Do work
CleanUpAndSave()

' Continue with shutdown
ShutdownBlockReasonDestroy(Me.Handle)

' Do shutdown
Dim p As New ProcessStartInfo("shutdown", "/s /t 0")
p.CreateNoWindow = True
p.UseShellExecute = False
Process.Start(p)

' Exit the application to allow shutdown (For some reason 'ShutdownBlockReasonDestroy'
' doesn't really unblock the shutdown command).
Application.Exit()
Return
End If

MyBase.WndProc(aMessage)
End Sub

However, I suggest that you don't suspend the shutdown command and start doing some work immediately without prompting a confirmation message to the user especially if that work takes several minutes.

Instead, you should display a message box explaining to the user that the application needs to do some work before the system shuts down, and let them decide whether to do that work or to shut down immediately. The system will inform the user that your application is preventing it from shutting down. If they care, they will click "Cancel" and read your message. If they don't care, they'll have the option to "Force shutdown" anyway whether you displayed the message or not.

Hope that helps :)

Android emulator won't shut down

According to the documentation I found at google, simply closing the
emulator window is the correct way to shut the emulator down.

To stop an emulator instance, just close the emulator's window.

Source: Starting and Stopping the Emulator



Related Topics



Leave a reply



Submit