0

I've got a Directory and a FileSystemWatcher and I want to reduce the number of Balloon.Show everytime I delete a file but how can I do? Because everytime I delete like 20 files explorer.exe crashes cause of the too ShowBalloonTips. There is a solution that I found in this forum that works if I delete the files one by one, but if I delete them all togheter it gets bugged and don't fire OnDelete no more... Any suggestion?

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 Answers2

0

It's not the most efficient way, but I would probably hold the number of files deleted and show the tooltip every nth time, like

private int deleteCounter = 0;

private void OnDeleted(object sender, FileSystemEventArgs e)
{
    //your delete code
    if(deleteCounter % 50 == 0)
    {
        SetBalloonTip("File Eliminato");
        notifyIcon1.ShowBalloonTip(1);
    }
}
Selim Balci
  • 940
  • 2
  • 11
  • 26
0

You better wait a bit before you show the Notification and prevent to much notifications to be pushed on the explorer.

The best way to do this is by using your timer to delay the notification by 500 ms. During that time you keep track of how many files got deleted in a simple counter. Once the Timer ticks, you show the Notification with the number of files deleted so far. You reset the counter and wait a bit for the notification to disappear. Then you're back in the initial state and the process can restart.

An implementation of above scenario might look like this:

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

I couldn't find a reliable way to detect if the notification is already gone, hence the extra wait states before I reset back to the initial state. If your users really care about the exact number of deleted files you might want to tighten the housekeeping around the number of deleted files reported. I leave that as an excercise.

rene
  • 41,474
  • 78
  • 114
  • 152