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

Visual Basic.NET

Avatar of undefined
Last Comment
cano63
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.
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

I dont think there is an alternative. You can maintain history of files processed.

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

ASKER

Yes it is enable, but is not working
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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

ASKER

One second, i put a breakpoint inside the timer code
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of cano63
cano63

ASKER

It works
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo