Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Avoiding multiple instances

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone....

What's the best way to disallow launching of multiple instances of my GUI application? Static flag, checked in main() method? Something else?

Thoughts? Questions? Comments?

Thanks!
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javaoops,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the best practice would be to use "Singleton Pattern" design for your GUI class...

That is "Singleton Pattern" allows only one Instance for a particular class...

a.)
This is achieved by making the Constructor of the respective class as private method
There by no one can intialize the object in a routine way becoz the visibility of the
constructor is private..

b.)
To create the instance of that singleton class is done by providing a Static method which
returns the object of the class.
The logic for providing only single instance should go here in the Static method...

I can provide more info providing more details about the context and what is ur exact requirement.



Satish
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, synching on a physical file. Watch out for crashes as the file would still exist and you'd never be able to open the app again!
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I hope, this link will fulfill your requirement.

http://coding.derkeiler.com/Archive/Java/comp.lang.java.gui/2004-09/0651.html
 
Tom McAmmond
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's true.... It occurred to me that since my app runs in the taskbar using the JDIC taskbar code (jdic.dev.java.net) - ppl would likely just shut down their machine without properly quitting the program, and the lock file would stay in place. I must find a better way..... Thanks for the suggestions so far though! I'll post if I find another method
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing you can do is open a ServerSocket for listening at a specific port that only your app uses. If the attempt fails, your app is apparently already running, so you exit. All the singleton-based solutions only work within a single JVM, you realize -- to actually prevent multiple copies, you need to use some kind of OS resource, be it a file, socket, or something else.
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic