Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating single instance for system tray in java

I am working in a desktop application created in java. we are working for windows version (for running on windows O.S.) of the application. When we run our application, it creates a tray icon on desktop by using systemtray as

SystemTray tray = SystemTray.getSystemTray();

I am creating single instance of the system tray, which I am creating in the main class of the application.

I am creating exe for my application. My problem is when i am running the exe file of my application, it creates tray icon every time.

I want in my application only one tray icon present on desktop and running as a service. this icon should only get removed, when application is uninstalled. It should run as service in the background.

I am not getting any way which will help me to run my application as a service and there should be only one instance running in the background.

I want to run my application by creating a tray icon as a service and should have single instance present. Please guide me in this issue.

Thanks for your all valuable suggestions in advance.

like image 954
Toman Avatar asked Oct 14 '22 21:10

Toman


1 Answers

As Mudassir said, you should add a check when you start your program that tries to find another running instance of your app. This could be done by creating a specific file upon startup, and deleting it on close. If the file is already present when you start your app, it means that another instance is running. You could also use inter-process communications, but it would be more difficult than a simple file.

For the "service" part, you could hide the main window on startup, so your app will run as if it was a windows service.

like image 197
Bastien Jansen Avatar answered Oct 18 '22 05:10

Bastien Jansen