Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8710269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:35:06+00:00 2026-06-13T04:35:06+00:00

why does the FileSystemWatcher fire twice? Is there an easy way to fix it?

  • 0

why does the FileSystemWatcher fire twice? Is there an easy way to fix it? Surely if I update or edit the text file it should only fire once?

this link here http://weblogs.asp.net/ashben/archive/2003/10/14/31773.aspx says

  1. Events being raised twice – An event will be raised twice if an event handler (AddHander FSW.Created, AddressOf FSW_Created) is
    explicitly specified. This is because, by default, the public events
    automatically call the respective protected methods (OnChanged,
    OnCreated, OnDeleted, OnRenamed). To correct this problem, simply
    remove the explicit event handler (AddHandler …).

What does “remove the explicit event handler” mean?

Imports System.IO

Public Class Form2

    Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed

        'this fires twice
        MessageBox.Show("test")

    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        FileSystemWatcher1.Path = "C:\Users\c\Desktop\test\"
        FileSystemWatcher1.NotifyFilter = NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName Or NotifyFilters.CreationTime

        FileSystemWatcher1.IncludeSubdirectories = False
        FileSystemWatcher1.Filter = "text.txt"

    End Sub

End Class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:35:07+00:00Added an answer on June 13, 2026 at 4:35 am

    Update:

    I have come up with 2 solutions. One uses Threads, and the other doesn’t. Take your pick :-).

    Without threading:

    Imports System.IO
    
    Public Class Form1
        Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
            Dim watcher As System.IO.FileSystemWatcher = sender
            watcher.EnableRaisingEvents = False
    
            'Do work here while new events are not being raised.
            MessageBox.Show("Test")
    
            watcher.EnableRaisingEvents = True 'Now we can begin watching for new events.
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            FileSystemWatcher1.Path = "C:\Users\c\Desktop\test"
            FileSystemWatcher1.NotifyFilter = NotifyFilters.LastWrite
            FileSystemWatcher1.IncludeSubdirectories = False
            FileSystemWatcher1.Filter = "test.txt"
    
    
        End Sub
    
        Private Sub FileSystemWatcher_OnChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
        End Sub
    
    End Class
    

    This solution (without threading), sets the watcher.EnableRaisingEvents to False. It is after this point where you would normally process whatever files are affected (or changed). It then sets the EnableRaisingEvents back to True after your work is done.

    With threading:

    Imports System.IO
    
    Public Class Form1
        Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
            FileSystemWatcher1.EnableRaisingEvents = False
            Threading.Thread.Sleep(250)
            FileSystemWatcher1.EnableRaisingEvents = True
    
    
            MessageBox.Show("test")
    
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            FileSystemWatcher1.Path = "C:\Users\c\Desktop\test"
            FileSystemWatcher1.NotifyFilter = NotifyFilters.LastWrite
            FileSystemWatcher1.IncludeSubdirectories = False
            FileSystemWatcher1.Filter = "test.txt"
    
    
        End Sub
    
        Private Sub FileSystemWatcher_OnChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
        End Sub
    
    End Class
    

    This solution, although a bit hacky, does work. It disables checking for new changes/events for 250ms and then re-enables checking, based on the assumption that you won’t been needing to check for a change every 250ms. I have tried almost everything that I could think of to get a real solution for you but this will work in the meantime.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know if there is a way to generate different code in the
Does anyone out there know how to integrate ContentFlow ( http://www.jacksasylum.eu/ContentFlow ) and Lightbox2
Does any one know how to extract only div tags with particular class,in PHP?
Does anyone out there know what image format results in the fastest loading speeds?
Does anyone have any suggestions for the best / simplest way to view all
Does this line of Perl really do anything? $variable =~ s/^(\d+)\b/$1/sg; The only thing
does someone know how I can get Sublime Text 2 to auto-hide the tabs
Does anyone know why this bean is getting instantiated multiple times? I only ever
I'm looking for the equivalent of FileSystemWatcher for a specific MSMQ queue. Does something
I need a way to stop a worker thread that does not contain a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.