Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Avoiding multiple instances

 
Tom McAmmond
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!
 
Dirk Schreckmann
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!
 
satish sathineni
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
 
Jez Nicholson
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!
 
Javaoops
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic