簡體   English   中英

通知圖標未顯示在托盤C#Winforms中

[英]Notify icon does not show in tray C# winforms

當我啟動Windows窗體時,為什么通知圖標沒有顯示在系統任務欄中?
這是我在做什么。
我有一個Windows服務,它將啟動我的任務欄應用程序,該應用程序是Windows窗體應用程序(我正在使用模擬功能在當前用戶的上下文中啟動該應用程序)。 在任務欄應用程序中,我正在啟動一個包含通知圖標的表單。
這里的問題是通知圖標有時不會出現在系統任務欄中,並且我無法找到原因。
在窗體的OnLoad方法中,我將窗體的visible屬性設置為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);  
}

現在,我建議在“通知”圖標代碼或其他條件語句周圍設置一些斷點,以查找是否未調用某些內容。 嘗試找到一致的復制品,以幫助您診斷正在發生的事情。 過去我經歷了很多奇怪的事情,其中​​最令人困惑的是與定時事件有關

我也建議(如果確實與Thread.Sleep方法相關,那么這可能會解決您的問題,這是不理想的。用戶只是獲得了凍結的UI。使用計時器類之一並選擇滴答聲或經過的事件,以便用戶可以繼續進行,但是您仍然會延遲。

查看您的OnLoad方法,您會發現一個潛在的無限循環,如果拋出任何異常,則每5秒只能執行一次,記錄下來然后拋出異常。 當您使用Thread.Sleep您將阻塞UI線程,並且很可能會停止顯示通知圖標。 我將查看Activator.GetObject或RPC GetProbeStatus調用,並考慮將RPC代碼移至另一個線程以避免阻塞UI。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM