Restarting application

Hi, guys.
Tell me, how can I restart the application openFrameworks using the application code itself (on the condition of time)? OF0.8.3@VS2013

So just to be clear, you want to write an application that closes itself at a predetermined time and then somehow opens itself (while it’s closed) at a later time?

Yeah, man, that’s right.
In fact, I have another problem, but I can not invented a better solution.

You can close an app using ofExit() and use ofGetTimestampString() or ofGetElapsedTimef() to time it the system time or elapsed time. Restarting it is probably easier done using a shell script and scheduling it somehow depending on what your OS is.

Dear friend, I know how to set the condition for closing and call the function exit(). I was hoping that there is a method to start or restart the program in openFrameworks, something like app.restart(). Obviously, it is necessary to remake the program logic.
Anyway, thank you very much.

Normally once an application has exited, it no longer has any ability execute its code … since that code is no longer running.

Typically, a third party application will manage the starting and restarting of an application. On *nix we will sometimes use cron to schedule start / stop apps.

Some applications will appear to “auto update” themselves (download an update, close and restart), but these applications typically launch a secondary loader application (that is not shutdown when the original app exits) whose responsibility is to relaunch the newly updated app again.

So anyway, the answer is that there is no such daemon functionality etc built into openFrameworks. The easiest route on windows is probably to find a scheduler application that will relaunch an app at a predetermined time.

Maybe you can exit your oF app by looking at the clock … and restart it with one of these solutions?

did you try this ?

you could tweak the code if you neeed something a bit different :slight_smile:

So @Jordi this will open a second copy of the app and close the original one right?

yes. so in fact first will open a copy and then close the current app.
Not sure if that is what he needs.

Yes, I would like just such a functional.
And this addon almost works - it does not close the previous copy after open a new one. The first instance hangs until close a new copy, and then closes.

is the example what does not work? or your implementation?
are you in mac? win?

Can’t you just write a shell script that waits for 3 seconds before opening the application, and in OF call this script using system() or ofSystem() just before exiting?

The idea of the script is really simple and smart. But it does not work. Opens a new copy of the program and continues to hang the old copy.
Calling code:

if (some code)
    ofSystem("start " + ofFilePath::getCurrentExeDir() + "data\\Restart.bat");

Script code:

   timeout /t 5 /nobreak
   start my_programm.exe

This is how it is done in the addon.
Did you check if ofExit() is actually working for your application?
In my addon all I do is to call ofExit() that must not hang your app.
BTW in mac you need to add an “&” at the end of the ofSystem call. otherwise ofSystem is waiting to close the call in process.
did you try:

ofSystem("start " + ofFilePath::getCurrentExeDir() + "data\\Restart.bat &");

That would do the job in mac. not sure about win.
this works well for me in mac:

ofSystem("open " + ofFilePath::getCurrentExeDir() + "example_ofxRestartOFAppDebug");
1 Like

Oh, man, I’m writing for Шindows.

And by using ofxRestartOfApp and simple startup script all depends on the administrator rights - even if you give the program administrator rights, the script with admin rights, it does not want to run, but without the Windows admin rights is not runs many command lines - such as kill the process, for example.

on win
the original window didn’t close…
it just open the new window…

can’t find a restart code…

Yop !

If anyone is still looking for a solution on window here. Here is mine.

In your of app call a batch file.

ofDirectory dir("");
ofSystem("start " + dir.getAbsolutePath() + "/restartApp.bat");

and in your data folder, place the batch restartApp.bat

taskkill /f /IM yourProgramNam.exe
timeout /t 3
start "" ""yourProgramNam.exe""

It closes your app, wait for 3 seconds, start your program again and leave.
only tested on my W10 machine. It should work on other window version.


ofExit doesn’t actually exit the application, it seems. (Just closes the ofApplicationRunner event loop part?) So you can put some timing code in setup() & after calling ofExit().