Wrapping FileSystemWatcher to prevent crossthread ops

T

teslar91

I've been learning VB.NET for the past few weeks. One of the problems
I've run into is difficulties updating controls in events from certain
components, such as the FileSystemWatcher, that raise their events in
different threads.

I found plenty of examples on the Web of how to "detangle" the threads,
using Delegate functions, .InvokeRequired, and .Invoke. But all of
these examples demonstrated how to do it on a single form. I wanted to
do the detangling in the class that actually generates the events, so
that any form that uses that class will automatically work.

I found *no* examples for this. And a class doesn't have the needed
..InvokeRequired or .Invoke methods. It took me a few hours before I
figured out the rather obvious solution: just create a control *inside*
the class and use that for the needed methods.

As a result of this exercise, I now have a wrapper for the
FileSystemWatcher component, which I'm sharing the code for here. All
events from this wrapper are returned in the *proper* thread - the
thread that created the component.

Experts - please look over my code, and critique anything I could have
done better.

Everyone else - enjoy, and hope it helps someone. Watch out for
wordwrap.

-----

Public Class MyFileSystemWatcher
Inherits System.IO.FileSystemWatcher

Private frmobj As New System.Windows.Forms.Button

Public Shadows Event Changed(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs)
Public Shadows Event Created(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs)
Public Shadows Event Deleted(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs)
Public Shadows Event Disposed(ByVal sender As Object, ByVal e As
System.EventArgs)
Public Shadows Event [Error](ByVal sender As Object, ByVal e As
System.IO.ErrorEventArgs)
Public Shadows Event Renamed(ByVal sender As Object, ByVal e As
System.IO.RenamedEventArgs)

Private Delegate Sub DelegatorFSE(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs)
Private Delegate Sub DelegatorEA(ByVal sender As Object, ByVal e As
System.EventArgs)
Private Delegate Sub DelegatorEEA(ByVal sender As Object, ByVal e As
System.IO.ErrorEventArgs)
Private Delegate Sub DelegatorREA(ByVal sender As Object, ByVal e As
System.IO.RenamedEventArgs)

Private Sub Init()
Dim i1 As Integer = frmobj.Handle ' initialize control enough for
..InvokeRequired to work
AddHandler MyBase.Changed, AddressOf watch_Changed
AddHandler MyBase.Created, AddressOf watch_Created
AddHandler MyBase.Deleted, AddressOf watch_Deleted
AddHandler MyBase.Disposed, AddressOf watch_Disposed
AddHandler MyBase.Error, AddressOf watch_Error
AddHandler MyBase.Renamed, AddressOf watch_Renamed
End Sub

Sub New()
MyBase.New()
Init()
End Sub

Sub New(ByVal path As String)
MyBase.New(path)
Init()
End Sub

Sub New(ByVal Path As String, ByVal filter As String)
MyBase.New(Path, filter)
Init()
End Sub

Private Sub watch_Changed(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs)
If frmobj.InvokeRequired Then
frmobj.Invoke(New DelegatorFSE(AddressOf watch_Changed), sender,
e)
Else
RaiseEvent Changed(sender, e)
End If
End Sub

Private Sub watch_Created(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs)
If frmobj.InvokeRequired Then
frmobj.Invoke(New DelegatorFSE(AddressOf watch_Created), sender,
e)
Else
RaiseEvent Created(sender, e)
End If
End Sub

Private Sub watch_Deleted(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs)
If frmobj.InvokeRequired Then
frmobj.Invoke(New DelegatorFSE(AddressOf watch_Deleted), sender,
e)
Else
RaiseEvent Deleted(sender, e)
End If
End Sub

Private Sub watch_Disposed(ByVal sender As Object, ByVal e As
System.EventArgs)
If frmobj.InvokeRequired Then
frmobj.Invoke(New DelegatorEA(AddressOf watch_Disposed), sender,
e)
Else
RaiseEvent Disposed(sender, e)
End If
End Sub

Private Sub watch_Error(ByVal sender As Object, ByVal e As
System.IO.ErrorEventArgs)
If frmobj.InvokeRequired Then
frmobj.Invoke(New DelegatorEEA(AddressOf watch_Error), sender, e)
Else
RaiseEvent Error(sender, e)
End If
End Sub

Private Sub watch_Renamed(ByVal sender As Object, ByVal e As
System.IO.RenamedEventArgs)
If frmobj.InvokeRequired Then
frmobj.Invoke(New DelegatorREA(AddressOf watch_Renamed), sender,
e)
Else
RaiseEvent Renamed(sender, e)
End If
End Sub
End Class
 
T

teslar91

Well, I asked for a better way. I just noticed the
..SynchronizingObject property, which can automatically marshall events
to the correct thread for any form, control, or class I choose; and
which exists for not only FileSystemWatcher, but apparently all other
WinForms components that can raise events on foreign threads.

I will console myself by saying that this was at least a good exercise.
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top