Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start executing an exe file in windows, restart computer, and pick up where the process left off

Tags:

c#

.net

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.

like image 691
Mefhisto1 Avatar asked Oct 31 '22 22:10

Mefhisto1


1 Answers

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.

like image 102
Alex H Avatar answered Nov 15 '22 05:11

Alex H