Link to home
Create AccountLog in
Avatar of cano63
cano63

asked on

FileSystemWatcher and Timer enable

I having problem starting a timer from a FileSystemWatcher event,

I don't know what is happening but it don't start the timer in any way , I also try

Timer1.start but did not wok,  the problem is in the  FileSystemWatcher event, if i turn it on from a button it work fine.

Form load
watchfolder = New System.IO.FileSystemWatcher()
watchfolder.Path = C:\
AddHandler watchfolder.Created, AddressOf logchange
watchfolder.EnableRaisingEvents = True

Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
timer1.enable = true
End sub

Open in new window

Avatar of Vaughn Bigham
Vaughn Bigham
Flag of United States of America image

I am assuming that this is a System.Windows.Forms.Timer and that its declaration is in the visual form designer is that about right?

If that is the case, then the problem is likely due to the separate-threaded nature of the "logchange" callback. You could maybe ask the form to invoke a method that would start the timer (this might be a bad solution because I know the FileSystemWatcher can fire many many events).
(Untested Code)
 
Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
    If MyForm.InvokeRequired Then
        MyForm.Invoke(AddressOf StartTimer)
    End If
End sub

Private Sub StartTimer()
    timer1.Start()
End Sub

Open in new window


Another would be to declare a timer object as Shared and hopefully be able to start it from a different thread (without using the Forms designer).  Let me know if you want help doing something like that.

Best of Luck.
Is the Timer enabled?

One note of caution, Filesystem watcher raises events multiple times sometimes. Create a textfile and see for your self.
Avatar of cano63
cano63

ASKER

I saw what you said about the multiple events, is there any other thing that i can use to monitor a directory
I dont think there is an alternative. You can maintain history of files processed.

Is the timer enabled? Is it working?
Avatar of cano63

ASKER

Yes it is enable, but is not working
How do you know its not working? Have you stepped through it? What's the interval?
Avatar of cano63

ASKER

One second, i put a breakpoint inside the timer code
That is strange. Usually the reason is that the Timer's Enabled property is set to False by default. Does the timer event have Hanles clause with it?
ASKER CERTIFIED SOLUTION
Avatar of cano63
cano63

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of cano63

ASKER

It works