1

How to automatically restart c# console application after a succesfull run? My program should be running continuously. And i should be like a loop but not in the code cuz that doesn't work.

What i've tried:

Application.Restart();
Environment.Exit(0);

And

System.Environment.Restart();

None of these seems to work for my project. So i'm looking for other ways?

UwUs
  • 67
  • 1
  • 9
  • Do you mean you want the console app to restart *itself*, or for something else to restart the app? More context would be really useful. – Jon Skeet May 13 '22 at 09:42
  • What is the *actual* problem you want to solve? `Restart after executing` doesn't make much sense. Once a process exits, it exits. It can't affect itself because it no longer runs. If you want to execute the same code over and over, why not use a loop? If you want to schedule the application to run on a schedule, why not use Scheduled Tasks or cron ? – Panagiotis Kanavos May 13 '22 at 09:43
  • Your goal is quite unclear, but maybe you should have a look here: https://stackoverflow.com/questions/11931293/how-to-make-my-console-application-automatically-restart-after-a-succesful-run – Jonas Metzler May 13 '22 at 09:46
  • Applications that need to restart themselves are eg installers or auto-updating applications. They don't wait to exit though, they register themselves with Windows Restart Manager. Is that the case here? – Panagiotis Kanavos May 13 '22 at 09:47
  • Its for data logging on a industrial plant. where every second needs to be logged when data has changed. My code does that. But i was wondering what the best way is to do that so it runs 24/7 – UwUs May 13 '22 at 09:49
  • _"And i should be like a loop but not in the code cuz that doesn't work."_ Show us the loop and why is doesn't work. – Jeroen van Langen May 13 '22 at 09:50
  • Maybe it's better to switch from a console app to a system service or deamon – Marco Beninca May 13 '22 at 09:56

1 Answers1

0

There's a similar issue here for restating a console application here How restart the Console app?

Essentially you ask to start the application again.

If it's just that you want to run the same functionality you could use a loop or similar.

var shouldExit = false;
while(!shouldExit)
{
    // do work.
    var result = MethodToDoWork();

    // Get whether user wants to restart
    if(!result.Success)
    {
       shouldExit = true;
    }
}
simlawstu
  • 591
  • 5
  • 4