如何减少气球数

时间:2016-06-29 12:39:57

标签: c# directory filesystemwatcher

我有一个目录和一个FileSystemWatcher,我希望每次删除文件时减少Balloon.Show的数量,但我该怎么办?因为每次我删除像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();
    }

2 个答案:

答案 0 :(得分:0)

这不是最有效的方式,但我可能会删除已删除的文件数并每隔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);
    }
}

答案 1 :(得分:0)

您最好在显示通知之前稍等一下,并防止在浏览器上推送很多通知。

执行此操作的最佳方法是使用计时器将通知延迟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;
    }
}

我无法找到一种可靠的方法来检测通知是否已经消失,因此在重置回初始状态之前会有额外的等待状态。如果您的用户真正关心已删除文件的确切数量,您可能希望在报告的已删除文件数量周围收紧内务。我把它留作练习。