Restarting the application? Topic is solved

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
ChronoGraff
Knows some wx things
Knows some wx things
Posts: 42
Joined: Wed Apr 22, 2015 5:42 pm

Restarting the application?

Post by ChronoGraff »

Hi folks, I am looking to restart my application, is this possible?

What I mean is this; I have a check happening in main() and when that is false a "pin" window is opened. When a pin number is entered then I would like to restart the application. I just don't know how and I can't seem to find any resource here on that topic either.

Many thanks
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: Restarting the application?

Post by eranon »

Hello, To achieve this (auto-restart), you can create a launcher beside your main app. The main app calls the launcher and exits itself. The only goal of the launcher is to start the main app.
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
ChronoGraff
Knows some wx things
Knows some wx things
Posts: 42
Joined: Wed Apr 22, 2015 5:42 pm

Re: Restarting the application?

Post by ChronoGraff »

I'm not sure what you mean, a launcher? Is there an example? Many thanks
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Restarting the application?

Post by ONEEYEMAN »

Hi,
What eranon means is that you should have a shell script which have just one line in it.

Thank you.
ChronoGraff
Knows some wx things
Knows some wx things
Posts: 42
Joined: Wed Apr 22, 2015 5:42 pm

Re: Restarting the application?

Post by ChronoGraff »

I really appreciate the help guys but I am a little lost as to what you are both recommending :shock:

Shell script? Launcher? Excuse my ignorance folks.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Restarting the application?

Post by ONEEYEMAN »

It is all the same. ;-)

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Restarting the application?

Post by doublemax »

With "launcher" Eranon meant a small program that starts the main application.

However, you can also just start the same executable again and then terminate the old one. That means that for a short period of time, two instances of your program will be running. Therefore you should perform the restart only after you have closed all files etc. so that there are no conflicts.

I'm adding a modified version of the "minimal" sample that shows how to do it.
Attachments
minimal.cpp
(7.3 KiB) Downloaded 218 times
Use the source, Luke!
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: Restarting the application?

Post by eranon »

Yep, I meant as ONEEYEMAN and doublemax said... Through a shell script or a compiled executable, no matter.

Here a quick example (here, the app generates its launcher on fly -- not optimized, just for the concept):

Code: Select all

#include "wx/wx.h"
#include <wx/stdpaths.h>
#include <wx/filename.h>
#include <wx/textfile.h>

static const long ID_GO = wxNewId();
class MinDialog : public wxDialog
{
    public:
        MinDialog() : wxDialog(NULL, wxID_ANY, wxString("Minimal Dialog"),
                              wxDefaultPosition, wxSize(300, 300)){
            wxButton* btn = new wxButton(this, ID_GO, "RESTART");
            wxBoxSizer* szr = new wxBoxSizer(wxVERTICAL);
            szr->Add(btn, 1, wxEXPAND);
            this->SetSizer(szr);
            Connect(ID_GO, wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&MinDialog::OnGo);};
    private:
        void OnGo(wxCommandEvent& WXUNUSED(event))
        {
            // Example of auto-restart
            // NB: w/o error handling and any precaution (e.g. batch filename could be unavail, console should be hidden, antivirus may hurt, etc.)
            wxString strLauncher = wxFileName::GetTempDir() + "/mindlg_launcher.bat";
            wxTextFile f(strLauncher);
            f.Create();
            f.AddLine("timeout 2 /nobreak > nul");
            f.AddLine(wxString::Format("start %s", wxStandardPaths::Get().GetExecutablePath()));
            f.AddLine("exit");
            f.Write();
            f.Close();
            if (wxExecute(strLauncher, wxEXEC_ASYNC) > 0) Close(true);
        };
};

class MinApp : public wxApp{
    bool OnInit(){
        MinDialog dlg;
        dlg.ShowModal();
        return false;};};
DECLARE_APP(MinApp)
IMPLEMENT_APP(MinApp)
Otherwise, the doublemax's solution may be easier; depends on your app constraints (if it allows several instances, mainly).
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
ChronoGraff
Knows some wx things
Knows some wx things
Posts: 42
Joined: Wed Apr 22, 2015 5:42 pm

Re: Restarting the application?

Post by ChronoGraff »

Thanks loads for the solution! The file doublemax attached is perfect, although the other solution works too doublemax's fits perfectly and I understand it.

Thanks folks
Post Reply