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

LVL 1
cano63Asked:
Who is Participating?
 
cano63Author Commented:
Finaly I did the follow:

I configure the timer by defautl to be enable and create my code inside and if condition in the timer, this condition will execute if a boolena variable turn true. Then from the watcher sub i change the value of the variable to true or false
0
 
vbighamCommented:
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.
0
 
CodeCruiserCommented:
Is the Timer enabled?

One note of caution, Filesystem watcher raises events multiple times sometimes. Create a textfile and see for your self.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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

Is the timer enabled? Is it working?
0
 
cano63Author Commented:
Yes it is enable, but is not working
0
 
CodeCruiserCommented:
How do you know its not working? Have you stepped through it? What's the interval?
0
 
cano63Author Commented:
One second, i put a breakpoint inside the timer code
0
 
CodeCruiserCommented:
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?
0
 
cano63Author Commented:
It works
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.