Hey folks,

I'm using VB 2005 Express, obviously with .NET framework 2.0, and I've run into something a little odd..

I have myself a program which runs in the background, and uses a FileSystemWatcher. It logs changes to a certain directory, indicating that users have changed settings, etc..., etc..

Anyhow, one of the apps which changes settings does it in such a way that it deletes a file, then immediately creates a new file. So, to separate this type of event from a straight up deletion event, I put this code in the event handler (deletedlist is just an arraylist):

VB Code:
  1. Private Sub FW_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
  2.         deletedlist.Add(e.FullPath.ToUpper)
  3.         deletetime.Interval = 2000
  4.         deletetime.Enabled = True
  5.     End Sub
  6.  
  7.     Private Sub deletetime_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
  8.         deletetime.Stop() 'This is a "run once" timer.
  9.         For i As Integer = 0 To deletedlist.Count - 1
  10.             Dim SW As New IO.StreamWriter(path + "global.log", True)
  11.             Dim datestr As String = Date.Now.ToShortTimeString + " on " + Date.Now.ToShortDateString
  12.             SW.WriteLine(deletedlist(i).ToString + " was deleted at " + datestr)
  13.             SW.Close()
  14.             Threading.Thread.Sleep(500) 'Avoid windows file conflicts.
  15.         Next
  16.         deletedlist.Clear() 'Remove all items from list, as they've been handled.
  17.     End Sub

You will notice neither sub has a handles clause at the end. Fair enough. Here they are in the forms load event:

VB Code:
  1. AddHandler FW.Deleted, AddressOf FW_Deleted
  2.  AddHandler deletetime.Tick, AddressOf Me.deletetime_Tick

So, you would think this would work eh? The tick event never fires. Now, Sometimes I think I know what I'm doing, so I tried just dragging and dropping instead of using AddHandler, just incase I've got that all wrong. Still no dice.

I then started a new project, drag and dropped a winforms timer, and a filesystemwatcher object onto the form, and basically did the same thing without any other code, and it never fires the tick sub. I remove the code from the filesystemwatcher's event handler, stick it in a generic button click sub, and it works fine. Logically, next, I put a breakpoint on the filesystemwatcher's event code - as soon as I delete a file, boom, it fires. Line by line I step through the enabling and setting of the interval.. yet the tick event (which I've also got a breakpoint at) never fires.


Finally, I replace my Winforms timer with a System.Timers.Timer, change the words tick to elapsed, and everything's fine.

Any clue why the winforms timer.Tick event will not fire if Enabled is turned on by a deleted event of a FileSystemWatcher?

Bill