hakermania
November 6th, 2010, 02:18 AM
There is always the way to check for a file at application start, and if not found to create it and launch the program normally, and if found to say that the program is already running! And then on the close of the program to delete this file.
So you start the program, the file is created, and if another try of launching the program is made then you find the file, so you say that the program is currently running!
But this have a very big disadvantage! If your program force quits, then the file won't be deleted, and so the application would not be able to be launched unless the file is deleted by hand (something that can be done usually only by experts of the specific program)!
So, I figured out a better solution. the code is in C++ using QtCreator:
char p=getenv("USER");
system("pidof MyApp > /home/$USER/.config/MyApp/Checks/concurrent; launched=`awk '{print NF-1}' /home/$USER/.config/MyApp/Checks/concurrent`; if [ $launched -ne 0 ]; then echo \"Close\" > /home/$USER/.config/MyApp/Checks/concurrent; fi");
char homepid[150]="/home/";
strcat(homepid,p);
char otherpid[100]="/.config/MyApp/Checks/concurrent";
strcat(homepid,otherpid);
char linepid [ 128 ];
int ipid;
FILE *filepid = fopen ( homepid, "r" );
for ( ipid = 0; ipid < 1; ++ipid ) //getting the first line...
{
fgets ( linepid, sizeof linepid, filepid );
}
fclose ( filepid );
if(!strcmp (linepid,"Close\n")){system("echo \"\" > /home/$USER/.config/MyApp/Checks/concurrent"); QMessageBox::information(0, "Error!", "MyApp is already running!");};
What this source actually does is to check how many spaces does the pidof <MyApp> has!
pidof MyApp gives all the IDs of the process(es) named MyApp, so if they are 2, one space will exist etc. So, i check the number of spaces and if found something else than zero then it says that the program is allready running :) Simple I think :)
So you start the program, the file is created, and if another try of launching the program is made then you find the file, so you say that the program is currently running!
But this have a very big disadvantage! If your program force quits, then the file won't be deleted, and so the application would not be able to be launched unless the file is deleted by hand (something that can be done usually only by experts of the specific program)!
So, I figured out a better solution. the code is in C++ using QtCreator:
char p=getenv("USER");
system("pidof MyApp > /home/$USER/.config/MyApp/Checks/concurrent; launched=`awk '{print NF-1}' /home/$USER/.config/MyApp/Checks/concurrent`; if [ $launched -ne 0 ]; then echo \"Close\" > /home/$USER/.config/MyApp/Checks/concurrent; fi");
char homepid[150]="/home/";
strcat(homepid,p);
char otherpid[100]="/.config/MyApp/Checks/concurrent";
strcat(homepid,otherpid);
char linepid [ 128 ];
int ipid;
FILE *filepid = fopen ( homepid, "r" );
for ( ipid = 0; ipid < 1; ++ipid ) //getting the first line...
{
fgets ( linepid, sizeof linepid, filepid );
}
fclose ( filepid );
if(!strcmp (linepid,"Close\n")){system("echo \"\" > /home/$USER/.config/MyApp/Checks/concurrent"); QMessageBox::information(0, "Error!", "MyApp is already running!");};
What this source actually does is to check how many spaces does the pidof <MyApp> has!
pidof MyApp gives all the IDs of the process(es) named MyApp, so if they are 2, one space will exist etc. So, i check the number of spaces and if found something else than zero then it says that the program is allready running :) Simple I think :)