Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
We want to design a application , let X.jar
If we first time run it than the new window should open after
that if we run it than no new instance should be create , application must communicate 
with already running instance .


What I have tried:

We want to design a application , let X.jar
If we first time run it than the new window should open after
that if we run it than no new instance should be create , application must communicate
with already running instance .
Posted
Updated 12-Apr-16 23:48pm

1 solution

This is the classic case of having a single instance of an application. This is achieved by using mutex.

have a mutex in your Main class. when the application starts, check if this mutex is available, if yes then acquire it and start the app. If not, just exit the main method.

Now when you are closing the application, release the mutex.

So the first instance:
check the mutex.
since it is free, it will acquire and start the app.

Second instance:
check the mutex.
its not available, so just exit(possibly with user message).

First instance:
when closing, release the mutex so that next time it could start.

This is the general algorithm to solve this problem. You can find java specific code for this now.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900