FileSystemWatcher, Application closes
FileSystemWatcher, Application closes
FileSystemWatcher, Application closes
FileSystemWatcher, Application closes
FileSystemWatcher, Application closes
FileSystemWatcher, Application closes FileSystemWatcher, Application closes FileSystemWatcher, Application closes FileSystemWatcher, Application closes FileSystemWatcher, Application closes FileSystemWatcher, Application closes FileSystemWatcher, Application closes FileSystemWatcher, Application closes
FileSystemWatcher, Application closes FileSystemWatcher, Application closes
FileSystemWatcher, Application closes
Go Back  Xtreme Visual Basic Talk > > > FileSystemWatcher, Application closes


Reply
 
Thread Tools Display Modes
  #1  
Old 06-24-2010, 03:00 PM
DarkPhane's Avatar
DarkPhane DarkPhane is offline
Centurion
 
Join Date: Aug 2009
Posts: 131
Default FileSystemWatcher, Application closes


I'm just putting together a trial run of using this class before implimenting it elsewhere - I like to try things out first and get them working.

I have the monitoring set up and it detects file changes, but when i try to log these changes on ANY control on the form the whole application just closes - no exceptions, no nothing. If I use a messagebox to notify then it works just fine.. is this normal/expected with this class?

I hate to post so much code but I have no idea what is going wrong so I can't reduce the amount of code as in my head it's all potentially the thing causing the trouble.

Code:
Imports System.IO
Imports System.Diagnostics

Public Class Form1
    Public overlordWatcher As FileSystemWatcher

    Private Sub StartMonitoringBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartMonitoringBtn.Click
        'new instance of class
        overlordWatcher = New FileSystemWatcher
        'which file to watch (.path)
        overlordWatcher.Path = FileToWatchTxt.Text

        'designate what you want to be notified about
        overlordWatcher.NotifyFilter = IO.NotifyFilters.Attributes Or IO.NotifyFilters.DirectoryName Or
            IO.NotifyFilters.LastAccess Or IO.NotifyFilters.LastWrite Or IO.NotifyFilters.FileName

        'set events for the above happening
        AddHandler overlordWatcher.Changed, AddressOf LogModify
        AddHandler overlordWatcher.Deleted, AddressOf LogModify
        AddHandler overlordWatcher.Created, AddressOf LogModify

        'Different one needed for renamed as it uses a different event arg
        AddHandler overlordWatcher.Renamed, AddressOf LogRename

        'start watching and other general changes
        overlordWatcher.EnableRaisingEvents = True
        StartMonitoringBtn.Enabled = False
        StopMonitoringBtn.Enabled = True


    End Sub

    Private Sub LogModify(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)

        If e.ChangeType = WatcherChangeTypes.Deleted Then
            ListBox1.Items.Add("Delete")   'shuts application    <---------------------------------------------------
        ElseIf e.ChangeType = WatcherChangeTypes.Created Then
            MessageBox.Show("created")  'messageboxes work and don't shut application <-----------------------------------------
        ElseIf e.ChangeType = WatcherChangeTypes.Changed Then
            MessageBox.Show("changed - attributes, size, security, Name")
        End If

    End Sub

    Private Sub LogRename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)

        MsgBox("Rename")

    End Sub

    Private Sub StopMonitoringBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopMonitoringBtn.Click

        overlordWatcher.EnableRaisingEvents = False
        StartMonitoringBtn.Enabled = True
        StopMonitoringBtn.Enabled = False

    End Sub
End Class


Thanks for any help you can offer,


Phane
Reply With Quote
  #2  
Old 06-24-2010, 03:29 PM
snarfblam's Avatar
snarfblamFileSystemWatcher, Application closes snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 896
Default

Just shooting in the dark here, but are you sure LogModify is executed on the UI thread? I think unhandled exceptions on background threads might exhibit this sort of behavior.
__________________
C# _VB.NET _
Reply With Quote
  #3  
Old 06-24-2010, 03:30 PM
AtmaWeapon's Avatar
AtmaWeaponFileSystemWatcher, Application closes AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

This won't fix it, but it will confirm or deny a suspicion I have.

Add this to the top of LogModify:
Code:
If Me.InvokeRequired Then
  MessageBox.Show("OH NO")
End If
Tell me whether you do or do not see the message box after making that change. (snarfblam is suspicious of the same thing I am.)
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 06-24-2010, 03:35 PM
DarkPhane's Avatar
DarkPhane DarkPhane is offline
Centurion
 
Join Date: Aug 2009
Posts: 131
Default

I get the 'OH NO' messagebox...
Reply With Quote
  #5  
Old 06-24-2010, 04:02 PM
AtmaWeapon's Avatar
AtmaWeaponFileSystemWatcher, Application closes AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Read the "Classic Marshalling" section of my accessing controls and variables from worker threads tutorial. (The opening post explains the technical reasons why this solution is needed, but it was geared towards people explicitly using different threads.)

Your event handler is going to need to marshall delegates to the UI thread to update the UI.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #6  
Old 06-24-2010, 04:06 PM
DarkPhane's Avatar
DarkPhane DarkPhane is offline
Centurion
 
Join Date: Aug 2009
Posts: 131
Default

Thanks, I'll take a look tomorrow, farr to late to be doing this now

Again, thanks for your help,


Phane
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
FileSystemWatcher, Application closes
FileSystemWatcher, Application closes
FileSystemWatcher, Application closes FileSystemWatcher, Application closes
FileSystemWatcher, Application closes
FileSystemWatcher, Application closes
FileSystemWatcher, Application closes FileSystemWatcher, Application closes FileSystemWatcher, Application closes FileSystemWatcher, Application closes FileSystemWatcher, Application closes FileSystemWatcher, Application closes FileSystemWatcher, Application closes
FileSystemWatcher, Application closes
FileSystemWatcher, Application closes
 
FileSystemWatcher, Application closes
FileSystemWatcher, Application closes
 
-->