PDA

View Full Version : Console Program Problem



ball
27th May 2006, 11:09
How can I handle cleanup to my application with just a single console window (no GUI), when the user click the 'X' button in the right-top corner? Thanks.

patrik08
27th May 2006, 11:32
I suppose event is console & GUI to


****.cpp




/* close class destructur */
void Shop_Main::closeEvent( QCloseEvent* e )
{
qt_unlink(LOCK_FILE_APPLICATION);
if (isOpen()) {
QStringList nametableall = getTableNames();
int totalsx = nametableall.size();
if (totalsx > 0) {
dumptofilesql(LAST_SQL_SESSION);
}
SqlLog("Class Shop_Main close.... on open");
} else {
SqlLog("Class Shop_Main close.... on not open");
}
DownDirRemove(DB_IMPORT_CACHEDIR);
e->accept();
}


****.h


protected:
void closeEvent( QCloseEvent* );
//



#include <QCloseEvent>

template file ... take the designer ui class top name & put to...
http://ppk.ciz.ch/qt_c++/qt/res.html ajax running only on ie6 :(
http://ppk.ciz.ch/qt_c++/qt/ reading ***.txt file

zlatko
27th May 2006, 11:40
I think it not be helfull for console app , becouse it havent QWidget

patrik08
27th May 2006, 11:42
then make a simple traditional class ~destructor...

rickbsgu
27th May 2006, 20:32
A console app typically doesn't have an event loop, and it most certainly doesn't have a windowing (windows or X) event loop. There is no way to intercept the close event without a windowing event loop, that I know of.

Closing a console window usually simply destroys its subprocesses.

Now, if you want to try to get way under the covers --- you might try to find your parent process (the console) and see if the os sends some sort of signal or interrupt you can intercept when the parent process gets a notification to shut down. You might be able to clean up and shut down, then. (I don't know of such a signal off the top of my head. You're in for a lot of OS specific research.)

Another approach might be to set a system hook that somehow knows your process and your parent process (you'd register it on startup), and can intercept raw events before they're dispatched to the desktop. When your console gets a WM_CLOSE (or system equivalent) for the console window, you can intercept it and shut down your process (but your process has to have a looping thread or something to act on it, of course.)

Non-trivial.

rickb

patrik08
27th May 2006, 22:04
A normal mail server make lock.file ....
by a new process check if this fake file (empty or put time(NULL)) have a filetime older or new .... and delete file...

if (!lockfile exist) {
create it...
} else {
exit apps...
}

or if (lockfile exist) {
check filetime and or delete...
}

this console apps have sqls db connect? or sockets?

rickbsgu
28th May 2006, 03:48
I'm not sure what either e-mail or sql connection has to do with the console.

When the OS opens a console, I know of no lock file that gets set up, or why it would need one. It would be extremely internal to the OS.

Let's try again - are you opening the console window from your program, or are you starting your program from within a console window?

rickb

ball
28th May 2006, 09:12
My code is copied as below, as you see i use the 'console mode' of QApplication, however I dont know where to put the cleanup code when the user simply press the 'X' button in console window.


//------------------------------------------------------------------------------
#include <QtGui>
#include <QtSql>
//------------------------------------------------------------------------------
#include "thread_main.h"
//------------------------------------------------------------------------------
bool initializeConnection()
{
QSqlDatabase qSqlDatabase=QSqlDatabase::addDatabase("QODBC","SOME_DB");
qSqlDatabase.setDatabaseName("some_db");
return qSqlDatabase.open();
}
//------------------------------------------------------------------------------
int main(int iMain,char** cMain)
{
QApplication qApplication(iMain,cMain,false);
if (!initializeConnection())
{
qDebug()<<"initializeConnection: "<<QSqlDatabase::database("SOME_DB").lastError().text();
}
ThreadMain threadMain;
threadMain.start();
return qApplication.exec();
}
//------------------------------------------------------------------------------

wysota
28th May 2006, 10:01
Two possible places:
1. SIGTERM signal handler if on unix
2. QApplication subclass destructor.

Remember that your app gets killed if you close the console window containing it.

BTW. Why are you spawning a separate thread? You don't need it...

patrik08
28th May 2006, 10:05
That old http://qt4ds-monkey.sourceforge.net/
code made its own CONSOLE. (http://ciz.ch/svnciz/dialog_qt/qt4_a/qt4ds-monkey/Components/QConsole/)
So can one it surely well control...

Then it gives to still bring
cron jobs around processe in order... Wincron on window http://www.tomasello.com/software/wincron/

Or ... in your code make connection by a class ... and destructor works....