5

我正在使用用 java 创建的桌面应用程序。我们正在为应用程序的 Windows 版本(用于在 Windows 操作系统上运行)工作。当我们运行我们的应用程序时,它使用 systemtray 在桌面上创建一个托盘图标

SystemTray tray = SystemTray.getSystemTray();

我正在创建系统托盘的单个实例,该实例是在应用程序的主类中创建的。

我正在为我的应用程序创建 exe。我的问题是当我运行我的应用程序的 exe 文件时,它每次都会创建托盘图标。

我希望在我的应用程序中只有一个托盘图标出现在桌面上并作为服务运行。只有在卸载应用程序时,该图标才会被删除。它应该在后台作为服务运行。

我没有任何方法可以帮助我将应用程序作为服务运行,并且应该只有一个实例在后台运行。

我想通过将托盘图标创建为服务来运行我的应用程序,并且应该存在单个实例。请在这个问题上指导我。

提前感谢您提出的所有宝贵建议。

4

3 回答 3

2

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.

于 2010-11-08T13:17:13.943 回答
1

使您的应用程序成为单一实例。请告诉您如何在 Java 中制作 .EXE 文件?

于 2010-11-08T12:02:17.140 回答
1

要显示托盘图标,您的应用程序必须正在运行。如果您的托盘图标出现两次(或更多),那么您的应用程序会运行多次。您必须自己为此添加支票。

您不想将您的应用程序作为 Windows 服务运行,因为通常这些服务无法访问 Windows 桌面。

于 2010-11-08T12:02:29.137 回答