Search code examples
c#notifyicon

NotifyIcon - prevent multiple database query


I have a NotifyIcon and I set balloon text with MouseMove event. The balloon text comes from a database. This results continuous database query.

private void notifyIcon1_MouseMove(object sender, MouseEventArgs e)
{
    //database operations.......
}

How can I prevent this? I want to set balloon text once when mouse on NotifyIcon.


Solution

  • Another approach would be to add a Timer to your Form and set its Interval to a delay like 1 second. This delay would be how often the user could hit the database. Setup a Flag that gets reset by the Timer and check it in your NotifyIcon event. Something like:

        private bool AllowUpdate = true;
    
        private void notifyIcon1_MouseMove(object sender, MouseEventArgs e)
        {
            if (AllowUpdate)
            {
                AllowUpdate = false; // don't allow updates until after delay
    
                // ... hit the database ...
                // ... update your text ...
    
                timer1.Start(); // don't allow updates until after delay
            }
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
            // reset so it can be updated again
            AllowUpdate = true;
            timer1.Stop();
        }