How Windows handles scheduled tasks when restarting computer?

I have an indefinitely running scheduled task that repeats every now and then and runs for few minutes. What happens when this task is running and I click restart or shutdown computer? Will Windows let it finish first before shutdown or will Windows kill the task and then shutdown? Is there a way to change the behavior?

I need to ensure that Windows lets the task to finish before shutting down and I couldn't find information about this. If Windows lets it finish the tasks I'm happy with that but if the Windows kills or aborts my task, I would like to know if there is an automatic Windows way to make Windows wait or is the only way to first stop the tasks manually and then shutdown.


You can't really do this. When Windows starts to shutdown, it tells all running applications (which have top-level Windows) that they have to finish up whatever they're doing because the lights are about to go out. Since Windows Vista, they get two seconds to finish up before the lights go out.
Applications can respond to this with a message, saying No, wait! I'm doing something important, which will be shown to the user. But the user can always click on the force shutdown button.
If your scheduled task can gracefully finish up its business in two seconds when you tell it to stop, you're probably going to be OK. If not, or if the application doesn't understand the messages sent to it, telling it that Windows is about to shutdown, then you're in trouble - and perhaps you need to find an alternative method of doing whatever it is you need this application to do.