Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2015-05

uniqueinstance

5 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Martin

Hi,

Just need a little advice on placement of code please, thanks in advance.

I wanted my app to have only one instance, so found uniqueinstance - http://wiki.lazarus.freepascal.org/UniqueInstance (it has the ability to pass the command line parameters from the second to the first instance which is something I want but haven't got round to testing yet!)

There's no problem with uniqueinstance, from reading the source I think that it's terminating the second instance before the onshow event of the main form so I'm guessing that the formcreate and formdestroy events of the main form from the second instance still occur and that's where I think my problem is.

I am using the formcreate event to parse the command line arguments to the constructor of my main class, which amongst other things unzips files to the /tmp directory. I then use the formdestroy to clean up, deleting the files in the /tmp directory.

If I start a second instance with the same command line parameters as the first, uniqueinstance terminates the second instance and no second form is seen on screen. However, all the files are deleted from /tmp (formdestroy) so they are no longer available to the first instance so it fails. (I know in a real life situation it would be unlikely that the second instance would be trying to open the same files as the first, but not impossible.)

So either I use some form of file locking or keep uniqueinstance and move my code. I just wanted some advice please, as to where people on the list would suggest I put the code to parse the command line parameters to the constructor of my main class and/or clean up code. I was looking at some of the application events in the myapp.lpr file but I am not sure if that is the right place? Any help gratefully received.

Thanks,

Martin Collins

Reply by email

luiz americo pereira camara
to Martin

Check for the property PriorInstanceRunning in FormDestroy

Luiz

2015-05-23 7:12 GMT-03:00 Martin :

Show quoted text 44 lines
Hi, Just need a little advice on placement of code please, thanks in advance. I wanted my app to have only one instance, so found uniqueinstance - http://wiki.lazarus.freepascal.org/UniqueInstance (it has the ability to pass the command line parameters from the second to the first instance which is something I want but haven't got round to testing yet!) There's no problem with uniqueinstance, from reading the source I think that it's terminating the second instance before the onshow event of the main form so I'm guessing that the formcreate and formdestroy events of the main form from the second instance still occur and that's where I think my problem is. I am using the formcreate event to parse the command line arguments to the constructor of my main class, which amongst other things unzips files to the /tmp directory. I then use the formdestroy to clean up, deleting the files in the /tmp directory. If I start a second instance with the same command line parameters as the first, uniqueinstance terminates the second instance and no second form is seen on screen. However, all the files are deleted from /tmp (formdestroy) so they are no longer available to the first instance so it fails. (I know in a real life situation it would be unlikely that the second instance would be trying to open the same files as the first, but not impossible.) So either I use some form of file locking or keep uniqueinstance and move my code. I just wanted some advice please, as to where people on the list would suggest I put the code to parse the command line parameters to the constructor of my main class and/or clean up code. I was looking at some of the application events in the myapp.lpr file but I am not sure if that is the right place? Any help gratefully received. Thanks, Martin Collins -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150523/2f81a387/attachment.html>

Reply by email

Martin
to luiz americo pereira camara

Brilliant, thanks very much. Feel a bit silly now as should have seen that!

Checked for the property on formdestroy only at first, which worked. Then realised I had a massive memory leak so checked for the property in the formcreate as well and everything works, no leaks.

Thank you for writing that component Luiz, really helpful ;-)

Best regards,

Martin Collins

luiz americo pereira camara wrote 64 lines
Check for the property PriorInstanceRunning in FormDestroy Luiz 2015-05-23 7:12 GMT-03:00 Martin < <mailto:>>: Hi, Just need a little advice on placement of code please, thanks in advance. I wanted my app to have only one instance, so found uniqueinstance - http://wiki.lazarus.freepascal.org/UniqueInstance (it has the ability to pass the command line parameters from the second to the first instance which is something I want but haven't got round to testing yet!) There's no problem with uniqueinstance, from reading the source I think that it's terminating the second instance before the onshow event of the main form so I'm guessing that the formcreate and formdestroy events of the main form from the second instance still occur and that's where I think my problem is. I am using the formcreate event to parse the command line arguments to the constructor of my main class, which amongst other things unzips files to the /tmp directory. I then use the formdestroy to clean up, deleting the files in the /tmp directory. If I start a second instance with the same command line parameters as the first, uniqueinstance terminates the second instance and no second form is seen on screen. However, all the files are deleted from /tmp (formdestroy) so they are no longer available to the first instance so it fails. (I know in a real life situation it would be unlikely that the second instance would be trying to open the same files as the first, but not impossible.) So either I use some form of file locking or keep uniqueinstance and move my code. I just wanted some advice please, as to where people on the list would suggest I put the code to parse the command line parameters to the constructor of my main class and/or clean up code. I was looking at some of the application events in the myapp.lpr file but I am not sure if that is the right place? Any help gratefully received. Thanks, Martin Collins -- _______________________________________________ Lazarus mailing list <mailto:> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150523/5069bda0/attachment.html>

Reply by email

Mark Morgan Lloyd
to Martin: Just need a little advice on placement of code please, thanks in advance. I wanted my app…
Martin wrote 34 lines
Hi, Just need a little advice on placement of code please, thanks in advance. I wanted my app to have only one instance, so found uniqueinstance - http://wiki.lazarus.freepascal.org/UniqueInstance (it has the ability to pass the command line parameters from the second to the first instance which is something I want but haven't got round to testing yet!) There's no problem with uniqueinstance, from reading the source I think that it's terminating the second instance before the onshow event of the main form so I'm guessing that the formcreate and formdestroy events of the main form from the second instance still occur and that's where I think my problem is. I am using the formcreate event to parse the command line arguments to the constructor of my main class, which amongst other things unzips files to the /tmp directory. I then use the formdestroy to clean up, deleting the files in the /tmp directory. If I start a second instance with the same command line parameters as the first, uniqueinstance terminates the second instance and no second form is seen on screen. However, all the files are deleted from /tmp (formdestroy) so they are no longer available to the first instance so it fails. (I know in a real life situation it would be unlikely that the second instance would be trying to open the same files as the first, but not impossible.) So either I use some form of file locking or keep uniqueinstance and move my code. I just wanted some advice please, as to where people on the list would suggest I put the code to parse the command line parameters to the constructor of my main class and/or clean up code. I was looking at some of the application events in the myapp.lpr file but I am not sure if that is the right place? Any help gratefully received.

I don't use UniqueInstance, but have put that sort of thing immediately after Application.Initialize terminating with halt if it finds it can pass params to an existing copy.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Martin Collins
to Mark Morgan Lloyd

Thank you. I'll have a look at that as well.

I previously thought I should leave the project lpr file alone and let lazarus manage it, but it just seems a regular pascal source file. I have edited it previously when I set up SetHeapTraceOutput for the leakview package (after reading a tutorial) and my World didn't explode
- but I will make a backup first before I play with it!

Regards,

Martin Collins

Mark Morgan Lloyd wrote
I don't use UniqueInstance, but have put that sort of thing immediately after Application.Initialize terminating with halt if it finds it can pass params to an existing copy.

Reply by email