I know I’ve seen this somewhere here before but my searches couldn’t seem to find it.
My application has an option that allows the user to set some application configuration changes. One of them requires the program to shut down and restart. How do I tell the system (Windows) to restart the app? Right now, I am making the configuration change, setting a NeedsRestart property, and calling ‘Quit’. Then in the App.Close event, I’m checking the NeedsRestart property and if it is ‘True’ I do an
Dim f as FolderItem = App.ExecutableFile
as the last statements. Is this the right way to do it or am I heading for trouble? If not right, what is the right way?
Here is the snipped from my code to restart the computer and restart the app. I wrote this years ago and hope they still work:
[code]case “Restart” #if TargetMacOS then
script = "tell application ““System Events”” " + EndOfLine + “restart” + EndOfLine + “end tell” + EndOfLine
tempShell.execute “/usr/bin/osascript -e’” + script + “’” #elseif TargetWin32 then
tempBoolean = ExitWindowsMBS(2)
'tempShell.Execute(“shutdown -r -t 0”) 'forum.xojo.com/26217-put-a-computer-in-hibernate-sleep-shutdown-standby-etc-modes/0#p218514 #elseif TargetLinux then
'Normally, the shutdown command is used and recommended for rebooting the system. This command can give Users advance warning so that they can save their work and cleanly terminating specific programs.
tempShell.execute “shutdown -r” #endif
case “RestartApp” #if not DebugBuild then
'This will launch a second copy of your application and quit the current one
'Whilst it will work on Windows I don’t believe it is xPlat #if TargetConsole then
‘f.launch doesn’t work! #elseif TargetWin32 or TargetLinux then
Quit #elseif TargetMacOS then
‘script = "tell application ““System Events”” " + EndOfLine + “open -n '” + app.ExecutableFile.NativePath + "’ + EndOfLine + “end tell” + EndOfLine
‘tempShell.execute "/usr/bin/osascript -e’" + script + "’"
tempShell.execute "open -n " + app.ExecutableFile.ShellPath
Quit #endif #endif[/code]
[quote=478022:@Dale Arends]Dim f as FolderItem = App.ExecutableFile
Out of curiosity, I guess this approach is not compatible with using a Mutex to avoid multiple instances of your application since a new instance is launched before the current one is closed, right?
For a Windows project we created a helper app. The main app would start the helper app, then quit. The helper app scans the Task List until the main app is no longer showing (meaning it has quit according to the operating system) and then does a FolderItem.Launch of the main app exe and then the helper app quits.
It’s worked very well for this app since occasionally serial ports don’t like to open when the machine is first powered up. So if we can’t reach devices after a few tries we restart the app and try again.