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 3698460
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:59:02+00:00 2026-05-19T04:59:02+00:00

I’m trying to write a simple program to monitor a folder for new files

  • 0

I’m trying to write a simple program to monitor a folder for new files in VB.NET 2010, and am having some trouble.

Here’s a simplified version of what my program looks like:

Imports System.IO

Public Class Main
    Public fileWatcher As FileSystemWatcher

    Sub btnGo_Click(sender As System.Object, e As System.EventArgs) Handles btnGo.Click
        '//# initialize my FileSystemWatcher to monitor a particular directory for new files
        fileWatcher = New FileSystemWatcher()
        fileWatcher.Path = thisIsAValidPath.ToString()
        fileWatcher.NotifyFilter = NotifyFilters.FileName
        AddHandler fileWatcher.Created, AddressOf fileCreated
        fileWatcher.EnableRaisingEvents = True
    End Sub

    Private Sub fileCreated(sender As Object, e As FileSystemEventArgs)
        '//# program does not exit when I comment the line below out
        txtLatestAddedFilePath.Text = e.FullPath
        '//# e.FullPath is valid when I set a breakpoint here, but when I step into the next line, the program abruptly halts with no error code that I can see
    End Sub
End Class

As you can see, I have a button which will initialize a FileSystemWatcher when clicked. The initialization works, and when I place a new file in the monitored directory, the program reaches the fileCreated sub. I can even see that e.FullPath is set correctly. However, it exits abruptly right after that with no error code (none that I can see, anyways). If I comment everything in the fileCreated sub out, the program continues running as expected.

Any ideas as to why it’s dying on me? Any help would be greatly appreciated. I’m fairly new to VS/VB.NET, so maybe I’m just making a silly mistake. Thanks!

  • 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-05-19T04:59:02+00:00Added an answer on May 19, 2026 at 4:59 am

    Could be a cross-thread operation exception.

    Try this:

    Private Sub fileCreated(sender As Object, e As FileSystemEventArgs)
      me.Invoke(New MethodInvoker(Function() txtLatestAddedFilePath.Text = e.FullPath))
    End Sub
    

    or (even better in your context), during fileWatcher initialization:

    fileWatcher = New FileSystemWatcher()
    fileWatcher.SynchronizingObject = me
    [...]
    

    Explanation:

    http://www.blackwasp.co.uk/FileSystemWatcher.aspx (see Preventing Cross-Thread Operations)

    Excerpt:

    By default, when the FileSystemWatcher
    object raises notification events, the
    delegate calls are made on a thread
    from the system thread pool. This will
    generally not be the same thread as
    that being used to control the form.
    As the demonstration application will
    require that the file changes be
    logged within a visual element of the
    form, using the allocated thread to
    modify the list box contents would
    result in a cross-threading operation
    and an IllegalOperationException being
    thrown.

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

Sidebar

Related Questions

No related questions found

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.