Win Forms - Display self Disappearing Notifications/PopUps in quick succession?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FaizanKazi
    New Member
    • Jun 2010
    • 3

    Win Forms - Display self Disappearing Notifications/PopUps in quick succession?

    Hi all!
    I really need to show neat and professional looking messages, like helper tips, or error notifications, in quick succession, often occurring before the first message has disappeared....

    I was trying to display those notifications in a ToolStripStatus Label, but whats happening with my code is I have to wait for my background worker to finish displaying one message, before the other can be displayed and this hangs the UI thread, completely crashing the program.

    Code:
    //This method allows users to display auto-disappearing messages using a BackGroundWorker (bgwStatus) and a ToolStripStatusLabel (tssLabel1)
     private void showNotification(string message, Color color, int barValue)
            {
                tssLabel1.ForeColor = color;
                tssLabel1.Text = message;
                tsPBar1.Value = barValue;
                if (bgwStatus.IsBusy)
                    bgwStatus.CancelAsync();
                while (bgwStatus.IsBusy)
                    Thread.Sleep(20);
                bgwStatus.RunWorkerAsync();
                //Thread waitForNotification = new Thread(waitForNotificationOpportunity);
                //waitForNotification.Start();
            }
    
    //In this Method the BackGround Worker Waits for about 2 seconds
    private void bgwStatus_DoWork(object sender, DoWorkEventArgs e)
            {
                for (int i = 0; i < 40; i++)
                {
                    if (e.Cancel || bgwStatus.CancellationPending)
                        return;
                    Thread.Sleep(50);
                }
            }
    
    //Once the Time period (2 secs) has elapsed, the Label and the progress bar are reset.
            private void bgwStatus_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                tssLabel1.Text = "";
                tsPBar1.Value = 0;
            }
    I'd really appreciate the help. Thanks!!
    Last edited by Niheel; Jun 19 '10, 01:44 AM. Reason: please use code tags to display code
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    A couple of things...

    1) Try a message queue. Instead of having your GUI thread wait to show the next message, create a new class to hold messages in a queue with a timer. Whenever the timer ticks, it changes the message. You can still do this with a background worker, just have the timer govern the showing of messages and managing the queue.

    2) You might want to look into a NotifyIcon's ShowBalloonTip method for this... maybe that will help with your presentation?

    Comment

    • FaizanKazi
      New Member
      • Jun 2010
      • 3

      #3
      Thanks Gary!

      1)I thought about a message queue, I was just worried about messages piling up and not being written in good time...

      like if a user selects 4 different rows from a grid view in quick succession, and i'm taking 2 seconds to display each message about the gridrow selected, it'll take 8 seconds to display all the messages, the user might be confused when he gets backed up messages that dont relate to the most recent click. :s but it is a good solution, even if its not the sleekest.

      2) I shall check out NotifyIcon's ShowBalloonTip method, i'm just worried its a little more graphics intensive and windows 7 now hides notifications, so either the user will miss notifications or the computer might get bogged down if too many messages are piling up.

      Thanks so much for your help! i'll check out both solutions and get back to you about which seems more efficient. also, for any wanderers who look up this post. :)

      Comment

      • FaizanKazi
        New Member
        • Jun 2010
        • 3

        #4
        Originally posted by GaryTexmo
        A couple of things...

        1) Try a message queue. Instead of having your GUI thread wait to show the next message, create a new class to hold messages in a queue with a timer. Whenever the timer ticks, it changes the message. You can still do this with a background worker, just have the timer govern the showing of messages and managing the queue.

        2) You might want to look into a NotifyIcon's ShowBalloonTip method for this... maybe that will help with your presentation?
        So i tried the message queue! it worked well!! :)

        I made a custom class storing the (string) message, (Color) color of the message and (int) progress bar value.

        I made a Queue for my custom class and using a timer, I kept picking notifications off the Queue if there were any, otherwise setting my controls to their default values :)

        thanks so much for your help! :D

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          You're quite welcome, glad it worked out for you :)

          Comment

          Working...