C# Prevent multiple delayed NofityIcon BalloonTips overflow.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tyler Wiebe
    New Member
    • Mar 2011
    • 66

    C# Prevent multiple delayed NofityIcon BalloonTips overflow.

    I've come across a slight problem that I'm not sure how to fix.

    First I'll let you know that this is mainly because of one of my applications that has a notify icon, and it's connected to my iTunes, and when ever iTunes changes songs, my application gets notified, and then shows a balloon tip about the current song playing in iTunes.

    The problem is, if I step away from my computer, and another notify icon other than mine has a balloon tip, mine won't appear until my computer gets some sort of user input.
    This is fine... Unless I leave iTunes running for who knows how long.

    Let's say a balloon tip appears that isn't mine, and my application gets notified that iTunes has changed songs 100 times by the time that my computer gets user input again, this causes the balloon tip that isn't mine to disappear, and then mine to appear... But the first one, and then that one will disappear, and then it'll show the second one, and then disappear as well, and this keeps going until it reaches the current balloon tip that it should be showing, or the song changes again.

    Now I'm assuming the problem lies in how windows handles balloon tips, because awhile ago I read something about balloon tips and how they work, and from what I remember, this is something to do with that.

    Anyway, if someone could suggest a way or tell me how to prevent multiple delayed balloon tips, that would be great...
    And if I'm out of luck, please let me know as well.
  • Tyler Wiebe
    New Member
    • Mar 2011
    • 66

    #2
    Well I think I've found a temporary solution to my problem.

    Code:
        public partial class Form1 : System.Windows.Forms.Form
        {
            public System.Windows.Forms.NotifyIcon MyIcon { get; set; }
            public System.Windows.Forms.NotifyIcon OtherIcon { get; set; }
            public bool BalloonWaiting { get; set; }
    
            public bool UseDefaultMethod { get; set; }
    
            public Form1()
            {
                InitializeComponent();
    
                this.UseDefaultMethod = true;
    
                this.OtherIcon = new System.Windows.Forms.NotifyIcon();
                this.OtherIcon.Icon = this.Icon;
                this.OtherIcon.Visible = true;
    
                this.MyIcon = new System.Windows.Forms.NotifyIcon();
                this.MyIcon.Icon = this.Icon;
                this.MyIcon.BalloonTipShown += new System.EventHandler(NotifyIcon_BalloonTipShown);
                this.MyIcon.Visible = true;
            }
    
            public void NotifyIcon_BalloonTipShown(object sender, System.EventArgs e)
            {
                this.BalloonWaiting = false;
            }
    
            public void ShowBalloon(int Timeout, string Title, string Text, System.Windows.Forms.ToolTipIcon Icon)
            {
                if (string.IsNullOrEmpty(Title)) Title = " ";
                if (string.IsNullOrEmpty(Text)) Text = " ";
    
                this.MyIcon.BalloonTipTitle = Title;
                this.MyIcon.BalloonTipText = Text;
                this.MyIcon.BalloonTipIcon = Icon;
    
                if (this.MyIcon.Visible)
                {
                    System.Console.WriteLine(this.BalloonWaiting);
                    if (!this.BalloonWaiting)
                    {
                        this.BalloonWaiting = true;
                        this.MyIcon.ShowBalloonTip(Timeout);
                    }
                    else
                    {
                        this.MyIcon.Visible = false;
                        this.MyIcon.Visible = true;
    
                        this.MyIcon.ShowBalloonTip(3000);
                    }
                }
            }
    
            private void Form1_Click(object sender, System.EventArgs e)
            {
                this.OtherIcon.ShowBalloonTip(10000, "Blocking Application", "I'm blocking you.", System.Windows.Forms.ToolTipIcon.Info);
    
                if (this.UseDefaultMethod)
                {
                    this.MyIcon.ShowBalloonTip(3000, "Bob", "Says hello.", System.Windows.Forms.ToolTipIcon.Info);
                    System.Threading.Thread.Sleep(3000);
                    this.MyIcon.ShowBalloonTip(3000, "Bob", "Says hello again.", System.Windows.Forms.ToolTipIcon.Info);
                    System.Threading.Thread.Sleep(3000);
                    this.MyIcon.ShowBalloonTip(3000, "Bob", "Says hello once again.", System.Windows.Forms.ToolTipIcon.Info);
                    System.Threading.Thread.Sleep(3000);
                    this.MyIcon.ShowBalloonTip(3000, "Why", "Is bob saying hello so much.", System.Windows.Forms.ToolTipIcon.Info);
                }
                else
                {
                    this.ShowBalloon(3000, "Bob", "Says hello.", System.Windows.Forms.ToolTipIcon.Info);
                    System.Threading.Thread.Sleep(3000);
                    this.ShowBalloon(3000, "Bob", "Says hello again.", System.Windows.Forms.ToolTipIcon.Info);
                    System.Threading.Thread.Sleep(3000);
                    this.ShowBalloon(3000, "Bob", "Says hello once again.", System.Windows.Forms.ToolTipIcon.Info);
                    System.Threading.Thread.Sleep(3000);
                    this.ShowBalloon(3000, "Why", "Is bob saying hello so much.", System.Windows.Forms.ToolTipIcon.Info);
                }
            }
    The only problem with this is that there is a flicker in the system tray every so often when the icon visibility is toggled.

    Any improvements are welcome, and any better solutions are really welcome.

    Comment

    Working...