NotifyIcon - 防止多个数据库查询

NotifyIcon - prevent multiple database query

我有一个 NotifyIcon,我用 MouseMove event 设置气球文本。气球文本来自数据库。这导致连续的数据库查询。

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

我该如何防止这种情况发生?当鼠标在 NotifyIcon 上时,我想设置一次气球文本。

使用 BalloonTipShown 事件 (https://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.balloontipshown(v=vs.110).aspx) 您正在寻找的行为比 MouseMove 事件更符合该事件

另一种方法是在您的表单中添加一个计时器,并将其间隔设置为延迟 1 秒。这种延迟将是用户访问数据库的频率。设置一个由计时器重置的标志,并在您的 NotifyIcon 事件中检查它。类似于:

    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();
    }