3

I am developing an application on Qt symbian, in which I have to restart my application within my application, have used:

qApp->quit();
QProcess::startDetached(qApp->arguments()[0],qApp->arguments());

from a method in mainWindow. It is working fine on simulator but not on device, it closes but not restarting by itself, I have to restart it by myself, is there anything else I have to do to make it work on device.

abhishek
  • 857
  • 3
  • 13
  • 34
  • 2
    Did you try inverting the order of these two lines? – Mat Apr 21 '12 at 09:32
  • @Mat yes but stll the same problem.. :( – abhishek Apr 21 '12 at 09:54
  • What device are you using? Maybe there is specific option for your device. I suggest you try it on the another device – user123_456 Apr 21 '12 at 10:35
  • Given the order doesn't matter then if you take out the `qApp->quit()` call completely presumably it still doesn't work? – Troubadour Apr 21 '12 at 11:15
  • @denonth It is not working on other device also, I have tested it on nokia 5230 – abhishek Apr 21 '12 at 12:07
  • 1
    Can you check if `qApp->arguments()[0]` is not empty (you should be using `qApp->applicationFilePath()` to retrieve the application executable), and if starting the `QProcess` "attached" with `start()` doesn't return any error message ? – alexisdm Apr 21 '12 at 20:20
  • @alexisdm How to add applicationFilePath() bfore arguments()[0]? – abhishek Apr 23 '12 at 11:17
  • 1
    You have to replace `qApp->arguments()[0]` by `qApp->applicationFilePath()`, [the C/C++ main function `argv[0]` parameter can be empty or not the full path](http://stackoverflow.com/questions/2050961/is-argv0-name-of-executable-an-accepted-standard-or-just-a-common-conventi), `applicationFilePath()` should be more reliable, but you still have to check if any of these two methods returns the correct path. – alexisdm Apr 23 '12 at 19:54
  • @alexisdm I have replace it with qApp->applicationFilePath() and it is giving me the right path but not restarting the app.. :( Do I have to change something in main.cpp also ?? – abhishek Apr 24 '12 at 07:12
  • @alexisdm It shows Thread paniced error. I dont know why ? – abhishek Apr 24 '12 at 08:36

3 Answers3

2

One solution would be to create small console process that you can launch from your main program before closing it. Then this console process would just launch your program and close. I have been using this kind of processes to keep track of my apps and restart them when they crash.

Riho
  • 4,523
  • 3
  • 33
  • 48
1

One minor but fundamental thing: on Symbian there is an emulator and not a simulator. The difference is that the later simulates the device on the assembly level while the former does it only on API support level. For example iPhone simulator simulates the phone on assembly level. Contrarily in Symbian the underlying API implementation might be and is completely different for the ARM and for the WINS architecture. Especially in such cases when you interact with the OS like exiting the application.

The application quit operation on Symbian is eventually implemented by throwing a special exception (I don't remember it's name, something like KExitException) that is caught by the main Active Scheduler loop that tells the kernel to shut down the process. In other words it means that it is a synchronous call. If you first call quit then startProcess then the later will be never executed. It is not that clear why does not it work if you first call startProcess and then quit: this might be an asynchronous call that can not complete before you exit, or you simple can not start the same (GUI) application in two instances. Anyway check the return value of startProcess to see whether it succeeded or not.

Your ultimate solution will be to create a watchdog process as @Riho suggested. You start the watchdog process before you call quit, in the watchdog main function you wait some seconds and restart your application. You will need SwEvent capability for your watchdog.

MrTJ
  • 13,064
  • 4
  • 41
  • 63
0

I have tried it with Qprocess() and it seems to be working fine (still testing for memory and thread issues)

in main.cpp I write this code (which I got from other link )

int main(int argc, char *argv[])
{
    #define RESTART_CODE 1000

    int return_from_event_loop_code;
    QPointer<QApplication> app;
    QPointer<MainWindow> main_window;
    do
    {
        if(main_window) delete main_window;
        if(app) delete app;

        app = new QApplication(argc, argv);
        main_window = new MainWindow;
        QList<QString> lang = AppStatus::getCurrentLanguage();
        QTranslator translator;
        translator.load(lang.at(0));
        app->installTranslator(&translator);
        main_window->setOrientation(MainWindow::ScreenOrientationLockPortrait);


#if defined(Q_OS_SYMBIAN)
        main_window->showMaximized();
#else
        main_window->show();
#endif

        return_from_event_loop_code = app->exec();
    }
    while(return_from_event_loop_code==RESTART_CODE);

    return return_from_event_loop_code;
}

and in my method from where I have to restart my app I have written this.

QProcess::startDetached(qApp->applicationFilePath(),qApp->arguments());
qApp->exit(RESTART_CODE);

And my app is restarting like I wanted.. If any changes nedded plese let me know.

abhishek
  • 857
  • 3
  • 13
  • 34
  • it is showing "Thread 0x740 has panicked. Category: WSERV; Reason: 41" error on device after app restarts but not instantly, application starts normally but when I open some other screen it crashes with above mentioned error. – abhishek Apr 24 '12 at 08:06
  • `QProcess::startDetached` is probably not even doing anything at all, the restarting seems to be completely due to the loop. So you should have the same result with QProcess. Maybe you could keep the same `QApplication` instance by creating it outside the loop and create, show, and delete only the mainwindow in the loop. – alexisdm Apr 24 '12 at 10:59
  • @alexisdm It runs normal for the first time but if I call the method again it shows thread panicked error – abhishek Apr 24 '12 at 12:25
  • So maybe you can't rerun `QApplication::exec` multiple times in the same executable on Symbian, as [MrTJ](http://stackoverflow.com/a/10293570/894321) seems to suggest. (PS: I meant "without QProcess" in my previous comment). – alexisdm Apr 24 '12 at 12:27
  • @alexisdm I have tried it with excluding Qprocess(); I think I have to kill all threads before restarting it, is there any way to do it. – abhishek Apr 24 '12 at 12:52