explorer.exe崩溃后如何重新添加图标到系统托盘

发布于 2024-12-12 02:39:06 字数 280 浏览 0 评论 0原文

我的 java 应用程序当前在系统托盘中有一个图标。然而,当系统托盘崩溃时(您可以通过杀死任务栏中的explorer.exe来模拟这种情况),该图标会在系统托盘返回后消失。

当我检查 SystemTray.getSystemTray().getTrayIcons() 中的系统托盘中的图标时,它总是显示我的 TrayIcon 仍在系统托盘中,即使在它崩溃并重新加载之后并且我的托盘图标在那里更长。

目前,我每隔 15 秒左右删除和添加一次图标,但是有没有其他方法可以实现此目的,以便它不会不断消失和出现并烦扰用户?

My java application currently has an icon in the system tray. When the system tray crashes, however (you can simulate this by killing explorer.exe in the taskbar), the icon disappears after the system tray returns.

When I check SystemTray.getSystemTray().getTrayIcons() for the icons in the system tray, it always shows that my TrayIcon is still in the system tray, even though after it crashes and reloads and my tray icon is longer there.

At the moment I'm removing and adding my icon once every 15 seconds or so, but is there any other way of implementing this so that it won't keep disappearing and appearing and annoying the user?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

罪#恶を代价 2024-12-19 02:39:06

只是一个猜测。

每隔几秒检查一次 explorer.exe 的 pid。如果已更改,请取消注册托盘图标并重新注册。

Just a guess.

Check for pid of explorer.exe once in a few seconds. If it has changed, unregister your tray icon and register it again.

甜扑 2024-12-19 02:39:06

为了防止其他人偶然发现这个问题并想知道发生了什么,最后我不得不坚持在几秒钟后删除和添加图标,因为 Windows 不会更新托盘图标状态的 JVM...这自然不是最好的解决办法,但似乎也没有更好的办法了。

Just in case anyone else stumbles across this and wonders what happened, in the end I had to stick with removing and adding the icon after a few seconds, because Windows does not update the JVM on the status of the tray icons... It was naturally not the best way to fix it, but there didn't seem to be a better way.

黑凤梨 2024-12-19 02:39:06

这就是我修复它的方法。这是第一个答案的扩展。我更详细地讲一下。

我有一个正在运行的线程,它每 10 秒检查一次资源管理器的 pid(进程 ID)。如果进程 ID 发生变化,则会调用一个新方法来处理已消失的旧图标并添加一个新图标。

private volatile boolean isRunning = true;

public void startExplorerWatcherThread() {
    checkTrayIconThread = new Thread() {
        @Override
        public void run() {
            int prevProcessId = 0;
            int currProcessId = 0;
            while (isRunning) {
                try {
                    //Get the current process id from another method 
                    currProcessId = explorerPid();
                    // Set initial process ID
                    if (prevProcessId == 0) {
                        prevProcessId = currProcessId;
                    }
                    //thread sleeps for 10 sec and then checks again
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    System.out.println(e.toString());
                } catch (IOException e) {
                    System.out.println(e.toString());
                }


                //Check if the process id has changed or not
                if (prevProcessId != currProcessId) {
                    prevProcessId = currProcessId;
                    // call to the method that disposes the old icon and creates a new one and registers it
                    createNewTrayIcon();
                } else {
                    System.out.println("Windows Explorer stable.");
                }
            }
        }
    };
    //Start the thread
    checkTrayIconThread.start();
}

public int explorerPid() throws IOException {
    //List<Integer> used as might be more than one explorer process at a time
    List < Integer > pidArray = new ArrayList < > ();
    String pid;
    String bufferString;
    //Windows command prompt to retrive the pid of explorer process
    String cmd = "cmd.exe /c tasklist /FI \"IMAGENAME eq explorer.exe\ " /FO LIST | findstr \"PID:";
    //Execute cmd prompt
    Process cmdProcess = Runtime.getRuntime().exec(cmd);
    cmdProcess.getOutputStream().close();
    //Read the output of the execution
    BufferedReader stdout = new BufferedReader(new InputStreamReader(cmdProcess.getInputStream()));
    //Add all the pid Integer values to the list from output
    while ((bufferString = stdout.readLine()) != null) {
        pid = bufferString;
        pid = (pid.replaceAll("\\s", "")).replace("PID:", "");
        pidArray.add(Integer.valueOf(pid));
    }
    stdout.close();

    //Return lowest value from the array
    return Collections.min(pidArray);
}

This is the way I fixed it. This is an extension to the first answer. I go into a bit more detail.

I have a thread running which checks the pid (process ID) of the explorer each 10 seconds. If the process ID changes, a new method is called to dispose the old existing icon that has disappeared and adds a new one.

private volatile boolean isRunning = true;

public void startExplorerWatcherThread() {
    checkTrayIconThread = new Thread() {
        @Override
        public void run() {
            int prevProcessId = 0;
            int currProcessId = 0;
            while (isRunning) {
                try {
                    //Get the current process id from another method 
                    currProcessId = explorerPid();
                    // Set initial process ID
                    if (prevProcessId == 0) {
                        prevProcessId = currProcessId;
                    }
                    //thread sleeps for 10 sec and then checks again
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    System.out.println(e.toString());
                } catch (IOException e) {
                    System.out.println(e.toString());
                }


                //Check if the process id has changed or not
                if (prevProcessId != currProcessId) {
                    prevProcessId = currProcessId;
                    // call to the method that disposes the old icon and creates a new one and registers it
                    createNewTrayIcon();
                } else {
                    System.out.println("Windows Explorer stable.");
                }
            }
        }
    };
    //Start the thread
    checkTrayIconThread.start();
}

public int explorerPid() throws IOException {
    //List<Integer> used as might be more than one explorer process at a time
    List < Integer > pidArray = new ArrayList < > ();
    String pid;
    String bufferString;
    //Windows command prompt to retrive the pid of explorer process
    String cmd = "cmd.exe /c tasklist /FI \"IMAGENAME eq explorer.exe\ " /FO LIST | findstr \"PID:";
    //Execute cmd prompt
    Process cmdProcess = Runtime.getRuntime().exec(cmd);
    cmdProcess.getOutputStream().close();
    //Read the output of the execution
    BufferedReader stdout = new BufferedReader(new InputStreamReader(cmdProcess.getInputStream()));
    //Add all the pid Integer values to the list from output
    while ((bufferString = stdout.readLine()) != null) {
        pid = bufferString;
        pid = (pid.replaceAll("\\s", "")).replace("PID:", "");
        pidArray.add(Integer.valueOf(pid));
    }
    stdout.close();

    //Return lowest value from the array
    return Collections.min(pidArray);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文