Is this at all possible? If so, how? The flow would be something like:
private void DoStuff()
{
// Do some stuff
RestartPc();
}
private void RestartPc()
{
Process.Start("shutdown", "/r /t 0");
}
// Call this when the PC is restarted:
private void DoStuffAfterRestart() {}
All this code would be a windows service, so what would be the 'best' way to tell the OnStart
method to skip DoStuff
if pc was restarted and go straight to DoStuffAfterRestart
since service would be set to auto start.
Idk if there are special methods for this but i would have a solution:
Add your program to the autostart. EDIT: Like @PTwr pointed out you shouldn't use the normal autostart. Use the Run once registry key instead.
Change the code to something like so:
public static void main(string[] args)
{
var x = loadState();
if(x == null)
DoStuff();
else
DoStuffAfterRestart();
}
private void DoStuff()
{
// Do some stuff
SafeState();
RestartPc();
}
// Call this when the PC is restarted:
private void DoStuffAfterRestart() {}
In the safe/load methods you would have to write something to a file/registry to remember your current state
By saving you 'current position' you can 'remember' where you have been the last time and do the right stuff after a restart.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With