473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Wrapping FileSystemWatcher to prevent crossthread ops

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

Oct 10 '06 #1
1 2340
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.
:)

Oct 11 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Troy Murphy | last post by:
How do I prevent the FileSystemWatcher event to keep firing while the file is being created? When copying a file to the watched folder, the event fires a dozen or more times! Also, the...
4
by: abc | last post by:
This seems like a stupid question, but I can't find the property to prevent the text in a Label control from wrapping on the next line. Thanks.
4
by: Peter Lin | last post by:
Dear all; I need to monitor a xml file so that I can update my data in hashtable, but the problem is it will trigger more than one event for a lastwrite action(I trigger this action by add...
4
by: rodchar | last post by:
Hey all, I have a datagrid that auto-generate the columns. When I run the app some of the column values are wrapping. How do I prevent the values from wrapping? Do I have to use template...
20
by: J-T | last post by:
We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business...
0
by: Rich Wallace | last post by:
Hello all, Looking for suggestions and tips if possible. I have an application running on a file server that utilizes the FileSystemWatcher to trap when any Excel files are saved by a user. I...
1
by: marwan | last post by:
Is there any way (Using the FileSystemWatcher) to prevent the deletion of a certain file (or folder), i.e to cancel the delete process before it begins. Or atleast to recreate the deleted file...
4
by: Red2 | last post by:
Hi all, I have a little problem. I'm watching a directory on a server for a FTP file to come in. I'm using FileSystemWatcher to catch the file create event. Most cases this work fine. However...
1
by: D2 | last post by:
Hi, We are using FileSystemWatcher class in a windows service to monitor a directory "d:\abc". This path is configured in a config file. When the service is running and FSW is watching this...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.