[Lazarus] uniqueinstance

Mark Morgan Lloyd markMLl.lazarus at telemetry.co.uk
Sat May 23 15:10:37 CEST 2015


Martin wrote:
> 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]




More information about the Lazarus mailing list