开发者

Notify icon does not show in tray C# winforms

开发者 https://www.devze.com 2023-03-28 19:09 出处:网络
why does my notification icon does not show in system tray when i launch my windows form? Here is what i am doing.

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;
    /开发者_StackOverflow社区/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);  
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号