如何减少气球的数量

本文关键字:气球 何减少 | 更新日期: 2023-09-27 17:53:26

我有一个目录和一个FileSystemWatcher,我想减少气球的数量。每次显示我删除一个文件,但我该怎么办?因为每次我删除像20个文件explorer.exe崩溃的原因太ShowBalloonTips。有一个解决方案,我在这个论坛上发现的工作,如果我删除文件一个接一个,但如果我把它们一起删除,它会被窃听,不要火灾OnDelete不再…任何建议吗?

private void InitializeWatcher()
{
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
    watcher.Filter = "*.*";
    watcher.Changed += new FileSystemEventHandler(OnChanged);
    watcher.Created += new FileSystemEventHandler(OnCreated);
    watcher.Deleted += new FileSystemEventHandler(OnDeleted);
    watcher.Renamed += new RenamedEventHandler(OnRenamed);
    watcher.EnableRaisingEvents = true;
}
private void OnDeleted(object sender, FileSystemEventArgs e)
{
    SetBalloonTip("File Eliminato");
    notifyIcon1.ShowBalloonTip(1);
    ((FileSystemWatcher)sender).Deleted -= new FileSystemEventHandler(OnDeleted);
    timerRepeat.Interval = 500;
    timerRepeat.Elapsed += new ElapsedEventHandler(t_Elapsed);
    timerRepeat.Start();
    } 
  void t_Elapsed(object sender, ElapsedEventArgs e)
    {
        ((System.Timers.Timer)sender).Stop();
    }

如何减少气球的数量

这不是最有效的方法,但我可能会保留已删除的文件数量,并每n次显示一次工具提示,如

private int deleteCounter = 0;
private void OnDeleted(object sender, FileSystemEventArgs e)
{
    //your delete code
    if(deleteCounter % 50 == 0)
    {
        SetBalloonTip("File Eliminato");
        notifyIcon1.ShowBalloonTip(1);
    }
}

您最好在显示通知之前稍等片刻,以防止在浏览器上推送太多通知。

最好的方法是使用计时器将通知延迟500毫秒。在此期间,您可以在一个简单的计数器中跟踪删除的文件数量。计时器开始计时后,您将显示到目前为止已删除的文件数量的通知。您重置计数器并等待通知消失。然后返回到初始状态,进程可以重新启动。

上述场景的实现可能如下所示:

int pendingDeletes = 0;
int TipState = 0; // keeps the state of the notifyIcon, 
                  // 0 = initial,
                  // 1 is about to be shown
                  // above 1 is waiting to reset to 0
private void OnDeleted(object sender, FileSystemEventArgs e)
{
    pendingDeletes++;
    if (TipState == 0)
    {
        TipState++; 
        // it didn't want to start http://stackoverflow.com/a/18348878
        this.Invoke( new MethodInvoker( () => timer1.Start()));
    }
}
// tick every 500 ms
private void timer1_Tick(object sender, EventArgs e)
{
    Trace.WriteLine(TipState);
    switch(TipState)
    { 
        case 1:
            notifyIcon1.BalloonTipText = String.Format("{0} deleted file(s)", pendingDeletes);
            notifyIcon1.ShowBalloonTip(500);
            pendingDeletes = 0;
            TipState++;
            break;
        case 2:
            // do nothing
            TipState++;
            break;
        case 3:
            // maybe do something if pendingDeletes > 0
            timer1.Stop();
            // back to initial state
            TipState = 0;
            break;
        default:
            // prevent mishaps
            TipState = 0;
            break;
    }
}

我找不到一种可靠的方法来检测通知是否已经消失,因此在我重置回初始状态之前需要额外的等待状态。如果您的用户确实关心已删除文件的确切数量,那么您可能希望严格控制所报告的已删除文件数量。我把它留作练习