Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

C# Programming question: Detect that Windows 10 is about to reboot (restart) without warning

Is it possible to detect, programmatically (using C#), or through a utility or windows service, that Windows 10 is about to reboot (unscheduled)?

I am not necessarily talking about a reboot after Windows Update has installed an update, so for the purposes of this question assume that a legitimate system reboot is not a factor in this.  Just assume that Windows 10 is rebooting at random unannounced times.

I am interested in gathering as detailed information as I can about the last events that are happening **just before** my mouse pointer locks and the laptop reboots.

Related to this question - are there utilities that will absolutely prevent Windows10 from rebooting at random times WITHOUT some sort of permission from the end-user?  That way if I have some sort of warning or reboot prevention I can shutdown my running applications and save my work.

I am not concerned that my laptop wants to reboot.  It may have legitimate reasons to do so.  I just don't want it to be able to reboot (legitimate or otherwise) without checking with me first and letting me click "Yes" or "No". I want to impose this on top of the Windows 10 OS --  so that if it is possible to detect it I want to prevent it and have it alert me.
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A pending shutdown (as in "for changes to apply, you need to reboot - do it now or do it later?) and a shutdown already in progress (one with a time counter set to x seconds) are two different things. The latter is what the author talks about, i guess. Is that easily detectable?
In this case it is the latter
User generated image
Avatar of Tom Knowlton

ASKER

McKnife:

>>>Windows will not decide to reboot on its own - Agreed.

>>>99,9% of installers will ask before they reboot - Agreed.

>>>[ ... you install windows updates and don't do the reboot ... ] [ ... so it will be enforced after a few days while constantly warning you...]  - Agreed.

>>>[... there is no system wide setting that can be queried "are you about to reboot in x minutes?" ...] - Okay - so what does a "pending reboot" mean.  I've seen this check performed in code snippets.  Is it saying "I know there is a reboot scheduled, but I don't know when?"
David Johnson, CD, MVP - This makes sense to me.  I will try the steps you recommend and report back here.
Tom, I am not familiar with what Shaun brought up, you should see if you can try it. However, I remain pessimistic as to whether this will help in most cases, since some programs will even have their own timer running that runs a shutdown -r command at some point.
An example I can think of if is someone runs something like this against your computer (or the API equivalent)
shutdown \\PC -r -f -t 0

Open in new window

and OP basically wants this when it happens
shutdown -a

Open in new window

Shaun Vermaak:

Your information on "Detect reboot" and "Cancel reboot" is intriguing.  I'll play around with this and see what happens.  Since I don't know when my laptop is going to reboot next, it may be a while before I can test this in the real world scenario I described in my oringal post.  In other words - forcing my laptop to reboot artificially may not be the same thing as when it happens on its own.

Since I do not get any warnings AT ALL when the reboot happens, I am leaning towards ruling-out "Windows Update" as the culprit.  As McKnife indicated, Windows Update will warn you numerous times before rebooting your laptop.  That is not what is happening here.  It is (seemingly) random.

Since it is apparently random - that is why my focus is on detection.  Maybe I cannot control THAT it happens or WHEN it happens - but if I can gather reliable and consistent data (via Event Viewer or some other method) then I can begin to put a plan together on how to resolve this.  My gut is that it is probably a hardware problem (overheating?) except for the fact that it might go for much longer periods of time AFTER it randomly reboots with no problems at all!!!  So if it were overheating - you would think that once it started - it would happen over and over and closer together since whatever was overheating would still be HOT and not given a chance to cool down.
Also - I am not necessarily talking about relying upon Windows to tell me it is going to reboot.  It may not know the reboot is going to happen.

But - depending on the nature of the reboot - there may be a consistent chain of events that happen each time.  Right now those are unknown.  If I can discover them - I may be able to answer this question (plus the help I've received here is helping me to think about possibilities I had not known about)

I am thinking about making better use of the Event Viewer under Administrative Tools.  I have been looking at it after each reboot, but it does not seem to be consistent.  I either need more verbose reporting or I need a tool that is better equipped for troubleshooting problems like this than the Event Viewer is capable of.

I was taking a look at my Event Viewer -System Log (grouping by Level and sorting in descending order) and I noticed that this Critical Error is happeing quite a bit:

 <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
  <Provider Name="Microsoft-Windows-Kernel-Power" Guid="{331C3B3A-2005-44C2-AC5E-77220C37D6B4}" /> 
  <EventID>41</EventID> 
  <Version>3</Version> 
  <Level>1</Level> 
  <Task>63</Task> 
  <Opcode>0</Opcode> 
  <Keywords>0x8000400000000002</Keywords> 
  <TimeCreated SystemTime="2017-06-28T00:19:20.965271700Z" /> 
  <EventRecordID>28590</EventRecordID> 
  <Correlation /> 
  <Execution ProcessID="4" ThreadID="8" /> 
  <Channel>System</Channel> 
  <Computer>ender</Computer> 
  <Security UserID="66666666666 hidden by me 66666666666666" /> 
  </System>
- <EventData>
  <Data Name="BugcheckCode">0</Data> 
  <Data Name="BugcheckParameter1">0x0</Data> 
  <Data Name="BugcheckParameter2">0x0</Data> 
  <Data Name="BugcheckParameter3">0x0</Data> 
  <Data Name="BugcheckParameter4">0x0</Data> 
  <Data Name="SleepInProgress">0</Data> 
  <Data Name="PowerButtonTimestamp">0</Data> 
  <Data Name="BootAppStatus">0</Data> 
  </EventData>
  </Event>

Open in new window

David Johnson, CD, MVP:

The checkbox was not checked:

User generated image
Over heating would cause your problem.. Normally you will get a BSOD first though. Normally one looks at the event logs first, but then again many users don't even know about them.

many cpu's have what is called speed step that attempts to slow the cpu down when it is overheating. This can be disabled in the bios in many cases. Perhaps using something to monitor your cpu temperatures i.e. speedfan might help
Speedfan screenshots:

User generated image
User generated image
User generated image
Online report generated by speed fan:

User generated image
and what are your cpu temperatures? enable logging and view the log after the computer resets
I am not sure where the log file is for CPU temperatures.

I'll attach some of the generated files
pciidsdata.csv
SFLog20170629.csv
SFLog20170629.csv
SFLog20170630-0001.csv