Have the application restart

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
killerbot
Experienced Solver
Experienced Solver
Posts: 75
Joined: Wed Dec 26, 2007 1:13 pm

Have the application restart

Post by killerbot »

Is there a quick way to have your application restarted.

I have some *sort of* server application, but when the user changed it's configuration through a menu entry, the entire application should restart (will refactor so it can be done internally within the program [stopping threads, closing sockets, ..]), but I was wondering if I can not just call the Exit myself, like when the user would have selected the quit/exit menu entry, but in some way have wx register the application with the OS to have it restarted automatically.

Is such a thing possible ?
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Which platform?

I don't think there's a cross-platform way, but there's platform-specific ways
"Keyboard not detected. Press F1 to continue"
-- Windows
killerbot
Experienced Solver
Experienced Solver
Posts: 75
Joined: Wed Dec 26, 2007 1:13 pm

Post by killerbot »

Hi,

I'd like this behavior on windows and on linux.

Cheers.
Kraymer
Earned a small fee
Earned a small fee
Posts: 23
Joined: Wed Feb 04, 2009 1:32 pm
Location: Germany
Contact:

Post by Kraymer »

You could write a wrapper application.
Just use different possible return codes for (int)wxApp::OnExit in your server app so that the wrapper knows if it should shutdown, restart or do whatever.
extreme001
I live to help wx-kind
I live to help wx-kind
Posts: 192
Joined: Fri Dec 22, 2006 9:17 am
Location: Germany
Contact:

Post by extreme001 »

Try exit(0);

In order to restart your application you should use a guard which restarts the application if the application quits. firebird database uses it.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

i think you could just start your own application again using ::wxExecute.

You only have to make sure that you do it when you closed all open files your application uses, so that the two instances that exist for a short period of time don't conflict with each other.
Use the source, Luke!
protocol
Moderator
Moderator
Posts: 680
Joined: Wed Jan 18, 2006 6:13 pm
Location: Dallas, TX
Contact:

Post by protocol »

Kraymer wrote:You could write a wrapper application.
Just use different possible return codes for (int)wxApp::OnExit in your server app so that the wrapper knows if it should shutdown, restart or do whatever.
I'm pretty sure heuristic virus scanners will go nuts if your executable does the above.
/* UIKit && wxWidgets 2.8 && Cocoa && .Net */
QuRegExmm
wxPCRE & ObjPCRE - Regex It!
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r »

Write a helper app that only waits a moment and then starts your app.

Launch it from your app, your app exits
helper app waits for your app to exit and then restarts it
helper app exits
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

mc2r wrote:Write a helper app that only waits a moment and then starts your app.

Launch it from your app, your app exits
helper app waits for your app to exit and then restarts it
helper app exits
On UNIX at least, there is not need for such an helper app, the shell is your helper app :)

Code: Select all

wxExecute("sleep 5 && myapp");
"Keyboard not detected. Press F1 to continue"
-- Windows
aquawicket
Earned some good credits
Earned some good credits
Posts: 103
Joined: Sun Aug 05, 2007 5:49 am

Post by aquawicket »

I did something like this for make an updater for my app. It does require a help app. The parent app sends a WID command to the help app. I ripped some code out for ya to play with.

PARENT APP

Code: Select all

wxProcess temp;
temp.Open(wxString::Format(wxT("helper.exe --WID=%d"), ::wxGetProcessId()), wxEXEC_ASYNC);
temp.Detach();
HELPERAPP.h

Code: Select all

#include <wx/wxprec.h>
#include <wx/cmdline.h>
#include <wx/process.h>
#include <wx/wfstream.h>

const wxCmdLineEntryDesc gCmdLineDesc[] =
{
    { wxCMD_LINE_OPTION, wxT("id"), wxT("WID"), wxT("Windows Process ID"), wxCMD_LINE_VAL_NUMBER },
    { wxCMD_LINE_NONE }
}; 

/////////////////////////////////////////////////////////////////////////////////
class MyApp : public wxApp
{

public:
    virtual bool OnInit();
    virtual int OnRun(){::wxExit(); return 0;}
    virtual void OnInitCmdLine(wxCmdLineParser& parser);
    virtual bool OnCmdLineParsed(wxCmdLineParser& parser);

private:
    DECLARE_NO_COPY_CLASS(MyApp)
};

DECLARE_APP(MyApp)

#endif //APP_H

HELPERAPP.cpp

Code: Select all

//// App.cpp ////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////

#include "App.h" 

IMPLEMENT_APP(MyApp); 

bool MyApp::OnInit()
{ 
	    if ( !wxApp::OnInit() ) return false;
}

void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
{
    parser.SetDesc (gCmdLineDesc);
    parser.SetSwitchChars (wxT("-"));
}
 
bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
{
	parser.Parse();
	long value; 
	
	//We got a command to close eTrigger
    if(parser.Found(wxT("WID"), &value)){ 
	    if(wxProcess::Exists(value)){
			
			//Okay, then let's close parent.exe 
			int killError = wxProcess::Kill(value, wxSIGTERM);
			if(killError){
				if(killError == wxKILL_BAD_SIGNAL){msg = wxT("wxProcess::Kill Error: no such signal"); return true;}
				if(killError == wxKILL_ACCESS_DENIED){msg = wxT("wxProcess::Kill Error: permission denied"); return true;}
				if(killError == wxKILL_NO_PROCESS){msg = wxT("wxProcess::Kill Error: no such process"); return true;}
				if(killError == wxKILL_ERROR){
					wxMessageBox(wxT("An Error occured attempting to close parent.exe, the helper will now attempt to forcefully close parent.exe"));
					int killError2 = wxProcess::Kill(value, wxSIGKILL);
					if(killError2){
						msg = wxT("The Helper could not close parent.exe"); return true;
					}
				}
			}
			
		
//Now let's open the parent
wxProcess temp;
temp.Open(wxT("parent.exe"), wxEXEC_ASYNC | wxEXEC_NOHIDE);
temp.Detach();
Exit();
}
Phoenix_pl
Earned a small fee
Earned a small fee
Posts: 22
Joined: Wed Oct 05, 2005 1:24 pm

Post by Phoenix_pl »

You can also do something like "internal restart" that I have used in couple of my applications. All you need to do is to create empty and hidden top frame which will start your main classes. When you need to restart just call restart method in your hidden frame that will destroy all child frames, dialogs, threads, processes etc. and start them again.
Post Reply