通知图标不显示在托盘中 C# winforms

发布于 2024-11-30 17:17:31 字数 1336 浏览 2 评论 0原文

当我启动 Windows 窗体时,为什么我的通知图标没有显示在系统托盘中?
这就是我正在做的事情。
我有一个 Windows 服务,它启动我的托盘应用程序,它是一个 Windows 窗体应用程序(我正在使用模拟在当前用户的上下文中启动该应用程序)。 在托盘应用程序中,我正在启动一个包含通知图标的表单。
这里的问题是通知图标有时不会出现在系统托盘中,我无法找出原因。
在表单的 OnLoad 方法中,我将表单的可见属性设置为 false。 我还在做一些远程服务调用(如 ipc)。这是一个问题吗?
如何使我的通知图标始终出现在系统托盘中?
编辑:这是 OnLoad 函数的代码

protected override void OnLoad(System.EventArgs e)
{
    this.Visible = false;
    //Get some value from registry
    CheckForStealthMode();
    GetLoginType();
    bool GetProbeStatus = false;
    ServiceActivityInterface remoteMethods = null;
    do
    {
        try
        {
            remoteMethods = (ServiceActivityInterface)Activator.GetObject(typeof(ServiceActivityInterface), "tcp://localhost:18800/ServiceRemoting);
            ProbeStatus = remoteMethods.GetProbeStatus();
            GetProbeStatus = true;
        }
        catch (Exception exception)
        {
            GetProbeStatus = false;
            log.Error("Exception while getting the status of Probe:" + exception.Message);
        }
        finally
        {
            remoteMethods = null;
            if (!GetProbeStatus) 
            {
                Thread.Sleep(5000);
                log.Debug("Retrying to get the probe status.");
            }
        }
    } while (!GetProbeStatus);  
}

why does my notification icon does not show in system tray when i launch my windows form?
Here is what i am doing.
I have a windows service which launches my tray application which is a windows form application (I am using impersonation to launch that application in current user's context).
In tray application i am launching a form which contains notify icon.

The problem here is that the notify icon some times does not appear in system tray and i am unable to find out why.

In the OnLoad method of the form i am setting form's visible property to false.
Also i am doing some remoting service calls (like ipc). Is that a problem?

How do i make my notify icon to appear always in system tray?
EDIT: Here is the code for OnLoad function

protected override void OnLoad(System.EventArgs e)
{
    this.Visible = false;
    //Get some value from registry
    CheckForStealthMode();
    GetLoginType();
    bool GetProbeStatus = false;
    ServiceActivityInterface remoteMethods = null;
    do
    {
        try
        {
            remoteMethods = (ServiceActivityInterface)Activator.GetObject(typeof(ServiceActivityInterface), "tcp://localhost:18800/ServiceRemoting);
            ProbeStatus = remoteMethods.GetProbeStatus();
            GetProbeStatus = true;
        }
        catch (Exception exception)
        {
            GetProbeStatus = false;
            log.Error("Exception while getting the status of Probe:" + exception.Message);
        }
        finally
        {
            remoteMethods = null;
            if (!GetProbeStatus) 
            {
                Thread.Sleep(5000);
                log.Debug("Retrying to get the probe status.");
            }
        }
    } while (!GetProbeStatus);  
}

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

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

发布评论

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

评论(2

一页 2024-12-07 17:17:31

查看您的 OnLoad 方法,您会发现一个潜在的无限循环,如果抛出任何异常,该循环只能每 5 秒执行一次,并记录异常然后将其丢弃。当您 Thread.Sleep 时,您会阻塞 UI 线程,并且很可能会阻止通知图标的显示。我会查看 Activator.GetObject 或 RPC GetProbeStatus 调用,并考虑将 RPC 代码移至另一个线程以避免阻塞 UI。

Looking at your OnLoad method, you've got a potentially infinite loop that can only execute once every 5 seconds if there is any exeception thrown, with the exception logged then thrown away. When you Thread.Sleep you're blocking the UI thread and most likely stopping the notify icon from displaying. I would look at the Activator.GetObject or the RPC GetProbeStatus calls, and consider moving the RPC code to another thread to avoid blocking the UI.

信仰 2024-12-07 17:17:31

现在,我建议在“通知”图标代码或其他条件语句周围设置一些断点,以查找是否有某些内容未被调用。尝试找到一致的再现,以帮助您诊断发生了什么。我过去发生过一些奇怪的事情,其中​​最令人困惑的是与

我也建议的定时事件相关的事件(如果它确实与 Thread.Sleep 方法相关,这可能会解决您的问题,这并不理想。用户只是得到冻结的 UI 使用其中一个计时器类并获取滴答声或已过去的事件,以便用户可以继续,但您仍然会得到延迟。

For now I would suggest a few breakpoints around the Notify icon code or other conditional statements to find if something isn't being called. Try and find a consistent reproduction in order to help you diagnose what is going on. I have had strange goings on in past of which the most confusin was one linked to a timed event

I would also suggest (and this may solve your problem if it does relate the Thread.Sleep method that this is not ideal. The user just gets a frozen UI. Use one of the timer classes and pick up the tick or elapsed event so the user can carry on but you still get the delay your after.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文