NotifyIcon - 阻止多个数据库查询

时间:2017-05-17 11:44:05

标签: c# notifyicon

我有一个MouseMove event,我用private void notifyIcon1_MouseMove(object sender, MouseEventArgs e) { //database operations....... } 设置了气球文字。气球文本来自数据库。这导致了连续的数据库查询。

{- Data declaration predefined in Haskell -}
data Maybe_ a = Nothing_ | Just_ a deriving( Show )

{- Array declaration -}
data Array_ a = A [ ( Int, Maybe_ a ) ] deriving( Show )

我该怎样防止这种情况?我想在NotifyIcon上鼠标时设置一次气球文本。

2 个答案:

答案 0 :(得分:1)

使用BalloonTipShown事件(https://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.balloontipshown(v=vs.110).aspx) 您要查找的行为与MouseMove事件

相比更符合该事件

答案 1 :(得分:0)

另一种方法是在表单中添加一个Timer,并将其Interval设置为1秒的延迟。这种延迟将是用户击中数据库的频率。设置一个由Timer重置的标志,并在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();
    }