473,403 Members | 2,359 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,403 software developers and data experts.

FileSystemWatcher does not seem to be working

I have a directory on my site that I keep a bunch of text files in (this
directory is "/poetry/poems/"). The Application keeps the first line of each
of these files in an HttpApplicationState variable as a SortedList. When I
add or modify a file in this directory, I want to delete this
HttpApplicationState variable. I tried to do this using the following lines
of code in Global.asax.vb:
Private WithEvents poemfilewatcher As New
IO.FileSystemWatcher(HttpContext.Current.Server.Ma pPath("/poetry/poems/"))

Private Sub PoemDirModified(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs) Handles poemfilewatcher.Changed,
poemfilewatcher.Created, poemfilewatcher.Deleted
HttpContext.Current.Application.Lock()
HttpContext.Current.Application.Remove("poemlist")
HttpContext.Current.Application.UnLock()
End Sub

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
poemfilewatcher.IncludeSubdirectories = True
poemfilewatcher.EnableRaisingEvents = True
End Sub
The Function that I use to return this SortedList, whether it is stored in
an HttpApplicationState variable or not, is the following, which is also
located in Global.asax.vb:
Public Shared Function GetPoems() As SortedList 'Key=Title, Value=File
If HttpContext.Current.Application("poemlist") Is Nothing Then
Dim Poems As New SortedList(New CaseInsensitiveComparer)
Dim poemfiles As String() =
System.IO.Directory.GetFiles(HttpContext.Current.S erver.MapPath("/poetry/poems/"))
Dim poemstreamreader As System.IO.StreamReader
Poems.Capacity = 50 'Not quite 50 poems, so call Poems.TrimToSize()
before caching
For Each poemfile As String In poemfiles
poemstreamreader = System.IO.File.OpenText(poemfile)
Poems.Add(poemstreamreader.ReadLine(), poemfile)
poemstreamreader.Close()
Next
Poems.TrimToSize()
HttpContext.Current.Application.Lock()
HttpContext.Current.Application.Add("poemlist", Poems)
HttpContext.Current.Application.UnLock()
Return Poems
Else
Return CType(HttpContext.Current.Application("poemlist"),
SortedList)
End If
End Function
However, when I add, delete, or modify a file in the directory it does not
seem to delete the HttpApplicationState variable. Am I forgetting to do
something? Am I doing something wrong? Any help would be appreciated, or
possibly an example that mentions all the necessary & required steps. Thank
you to anyone who can give me any help with this.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Apr 21 '06 #1
6 1350
Nathan,

One thing I have learned from using the file system watcher previously
is that sometimes it's events trigger so soon after the file is created
/ modified that any code that references the file could be blocked as
the application that caused the creation / modification event still has
the file locked.

Try putting in a wait command for a few seconds to allow all locks on
the file to be cleared. Also I would specify a filter on the file
system watcher so that any files you don't want in there aren't picked
up.

The other thing is that from looking at your code it is possible the
file watcher could be going out of scope. Try looking at running the
file system watcher in a separate thread which is triggered by the
application start event, and ended by the application end event.

There is another way to implement this type of functionality which
would be to use the configuration sections of the web config, whenever
you add a file to the folder simply add it's first line of text and
filename to your sorted list. ASP.Net does automatically cycle when
the web config has been changed, this will ensure your list is always
updated accordingly, although it does add an extra degree of complexity
/ maintenance.

Apr 22 '06 #2
you have a couple issues.

1) you need to dedicate a thread to the filewatcher. currently you are using
the first request thread. if no request come for a couple a seconds. request
threads come from a pool. if not reused, the thread is killed.

2) you are using HttpContext.Current. this will not be valid unless the the
event is fired while processing a request.

you should start a background thread, pass it a reference to the context,
and process the event.

-- bruce (sqlwork.com)
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:uK**************@TK2MSFTNGP05.phx.gbl...
I have a directory on my site that I keep a bunch of text files in (this
directory is "/poetry/poems/"). The Application keeps the first line of
each of these files in an HttpApplicationState variable as a SortedList.
When I add or modify a file in this directory, I want to delete this
HttpApplicationState variable. I tried to do this using the following lines
of code in Global.asax.vb:
Private WithEvents poemfilewatcher As New
IO.FileSystemWatcher(HttpContext.Current.Server.Ma pPath("/poetry/poems/"))

Private Sub PoemDirModified(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs) Handles poemfilewatcher.Changed,
poemfilewatcher.Created, poemfilewatcher.Deleted
HttpContext.Current.Application.Lock()
HttpContext.Current.Application.Remove("poemlist")
HttpContext.Current.Application.UnLock()
End Sub

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
poemfilewatcher.IncludeSubdirectories = True
poemfilewatcher.EnableRaisingEvents = True
End Sub
The Function that I use to return this SortedList, whether it is stored in
an HttpApplicationState variable or not, is the following, which is also
located in Global.asax.vb:
Public Shared Function GetPoems() As SortedList 'Key=Title, Value=File
If HttpContext.Current.Application("poemlist") Is Nothing Then
Dim Poems As New SortedList(New CaseInsensitiveComparer)
Dim poemfiles As String() =
System.IO.Directory.GetFiles(HttpContext.Current.S erver.MapPath("/poetry/poems/"))
Dim poemstreamreader As System.IO.StreamReader
Poems.Capacity = 50 'Not quite 50 poems, so call Poems.TrimToSize()
before caching
For Each poemfile As String In poemfiles
poemstreamreader = System.IO.File.OpenText(poemfile)
Poems.Add(poemstreamreader.ReadLine(), poemfile)
poemstreamreader.Close()
Next
Poems.TrimToSize()
HttpContext.Current.Application.Lock()
HttpContext.Current.Application.Add("poemlist", Poems)
HttpContext.Current.Application.UnLock()
Return Poems
Else
Return CType(HttpContext.Current.Application("poemlist"),
SortedList)
End If
End Function
However, when I add, delete, or modify a file in the directory it does not
seem to delete the HttpApplicationState variable. Am I forgetting to do
something? Am I doing something wrong? Any help would be appreciated, or
possibly an example that mentions all the necessary & required steps.
Thank you to anyone who can give me any help with this.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Apr 22 '06 #3
Those all sound like good, working solutions. I think that the two that I
would probably use one of is either the one about the wait command or the
one about creating a separate thread. However, both of those involve code
that I do not have experience using. The one for the wait command sounds
like it is pretty simple, I just need to see the code to use for it. I have
some experience with threads and what they are, just not in VB.NET, so if
you could show me a basic example of how to use a thread in VB.NET or direct
me to a site with some good examples, I would appreciate it. Thank you SO
MUCH for your help and quick response to this problem. Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Peter Johnson" <pe***********@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Nathan,

One thing I have learned from using the file system watcher previously
is that sometimes it's events trigger so soon after the file is created
/ modified that any code that references the file could be blocked as
the application that caused the creation / modification event still has
the file locked.

Try putting in a wait command for a few seconds to allow all locks on
the file to be cleared. Also I would specify a filter on the file
system watcher so that any files you don't want in there aren't picked
up.

The other thing is that from looking at your code it is possible the
file watcher could be going out of scope. Try looking at running the
file system watcher in a separate thread which is triggered by the
application start event, and ended by the application end event.

There is another way to implement this type of functionality which
would be to use the configuration sections of the web config, whenever
you add a file to the folder simply add it's first line of text and
filename to your sorted list. ASP.Net does automatically cycle when
the web config has been changed, this will ensure your list is always
updated accordingly, although it does add an extra degree of complexity
/ maintenance.

Apr 22 '06 #4
Nathan,

The wait command should be used in tandem with creating a separate
thread. As it would happen to any thread the event is triggered upon.
Hard to believe but microsoft wrote the file system watcher and it is
Uber Efficient!!

Bruce has hit the nail on the head with creating a new thread so that
the file system watcher remains in scope regardless of the context of
the application. I'm unsure on how to implement the threading within
the web application, i've only done threading in windows services.

Apr 22 '06 #5
The responses that you and Peter have given me both seem like very good
advice, but like I have said, I don't really have any experience using
threads in VB.NET. This fact combined with what you have said about the
event needing to be fired while processing a request when used with ASP.NET
is making it kind of hard for me to figure out what to do about figuring out
a solution. Do you have any suggestions/ideas/recommended sites that could
help? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"bruce barker (sqlwork.com)" <b_*************************@sqlwork.com> wrote
in message news:%2****************@TK2MSFTNGP02.phx.gbl...
you have a couple issues.

1) you need to dedicate a thread to the filewatcher. currently you are
using the first request thread. if no request come for a couple a seconds.
request threads come from a pool. if not reused, the thread is killed.

2) you are using HttpContext.Current. this will not be valid unless the
the event is fired while processing a request.

you should start a background thread, pass it a reference to the context,
and process the event.

-- bruce (sqlwork.com)
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:uK**************@TK2MSFTNGP05.phx.gbl...
I have a directory on my site that I keep a bunch of text files in (this
directory is "/poetry/poems/"). The Application keeps the first line of
each of these files in an HttpApplicationState variable as a SortedList.
When I add or modify a file in this directory, I want to delete this
HttpApplicationState variable. I tried to do this using the following
lines of code in Global.asax.vb:
Private WithEvents poemfilewatcher As New
IO.FileSystemWatcher(HttpContext.Current.Server.Ma pPath("/poetry/poems/"))

Private Sub PoemDirModified(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs) Handles poemfilewatcher.Changed,
poemfilewatcher.Created, poemfilewatcher.Deleted
HttpContext.Current.Application.Lock()
HttpContext.Current.Application.Remove("poemlist")
HttpContext.Current.Application.UnLock()
End Sub

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
poemfilewatcher.IncludeSubdirectories = True
poemfilewatcher.EnableRaisingEvents = True
End Sub
The Function that I use to return this SortedList, whether it is stored
in an HttpApplicationState variable or not, is the following, which is
also located in Global.asax.vb:
Public Shared Function GetPoems() As SortedList 'Key=Title, Value=File
If HttpContext.Current.Application("poemlist") Is Nothing Then
Dim Poems As New SortedList(New CaseInsensitiveComparer)
Dim poemfiles As String() =
System.IO.Directory.GetFiles(HttpContext.Current.S erver.MapPath("/poetry/poems/"))
Dim poemstreamreader As System.IO.StreamReader
Poems.Capacity = 50 'Not quite 50 poems, so call
Poems.TrimToSize() before caching
For Each poemfile As String In poemfiles
poemstreamreader = System.IO.File.OpenText(poemfile)
Poems.Add(poemstreamreader.ReadLine(), poemfile)
poemstreamreader.Close()
Next
Poems.TrimToSize()
HttpContext.Current.Application.Lock()
HttpContext.Current.Application.Add("poemlist", Poems)
HttpContext.Current.Application.UnLock()
Return Poems
Else
Return CType(HttpContext.Current.Application("poemlist"),
SortedList)
End If
End Function
However, when I add, delete, or modify a file in the directory it does
not seem to delete the HttpApplicationState variable. Am I forgetting to
do something? Am I doing something wrong? Any help would be appreciated,
or possibly an example that mentions all the necessary & required steps.
Thank you to anyone who can give me any help with this.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/


Apr 22 '06 #6
> The responses that you and Peter have given me
If you wouldn't post to so many irrelevant groups, someone might have a clue
as to what Peter's response was.

microsoft.public.dotnet.framework.adonet,
microsoft.public.dotnet.framework.aspnet,
microsoft.public.dotnet.framework.aspnet.buildingc ontrols,
microsoft.public.dotnet.framework.aspnet.webcontro ls,
microsoft.public.dotnet.general,
microsoft.public.dotnet.l -> Apparently, you've reached some sort of limit
here

Bob Lehmann

"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:ug**************@TK2MSFTNGP05.phx.gbl...
The responses that you and Peter have given me both seem like very good
advice, but like I have said, I don't really have any experience using
threads in VB.NET. This fact combined with what you have said about the
event needing to be fired while processing a request when used with ASP.NET is making it kind of hard for me to figure out what to do about figuring out a solution. Do you have any suggestions/ideas/recommended sites that could
help? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"bruce barker (sqlwork.com)" <b_*************************@sqlwork.com> wrote in message news:%2****************@TK2MSFTNGP02.phx.gbl...
you have a couple issues.

1) you need to dedicate a thread to the filewatcher. currently you are
using the first request thread. if no request come for a couple a seconds. request threads come from a pool. if not reused, the thread is killed.

2) you are using HttpContext.Current. this will not be valid unless the
the event is fired while processing a request.

you should start a background thread, pass it a reference to the context, and process the event.

-- bruce (sqlwork.com)
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:uK**************@TK2MSFTNGP05.phx.gbl...
I have a directory on my site that I keep a bunch of text files in (this
directory is "/poetry/poems/"). The Application keeps the first line of
each of these files in an HttpApplicationState variable as a SortedList.
When I add or modify a file in this directory, I want to delete this
HttpApplicationState variable. I tried to do this using the following
lines of code in Global.asax.vb:
Private WithEvents poemfilewatcher As New
IO.FileSystemWatcher(HttpContext.Current.Server.Ma pPath("/poetry/poems/"))
Private Sub PoemDirModified(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs) Handles poemfilewatcher.Changed,
poemfilewatcher.Created, poemfilewatcher.Deleted
HttpContext.Current.Application.Lock()
HttpContext.Current.Application.Remove("poemlist")
HttpContext.Current.Application.UnLock()
End Sub

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
poemfilewatcher.IncludeSubdirectories = True
poemfilewatcher.EnableRaisingEvents = True
End Sub
The Function that I use to return this SortedList, whether it is stored
in an HttpApplicationState variable or not, is the following, which is
also located in Global.asax.vb:
Public Shared Function GetPoems() As SortedList 'Key=Title, Value=File
If HttpContext.Current.Application("poemlist") Is Nothing Then
Dim Poems As New SortedList(New CaseInsensitiveComparer)
Dim poemfiles As String() =
System.IO.Directory.GetFiles(HttpContext.Current.S erver.MapPath("/poetry/poe
ms/")) Dim poemstreamreader As System.IO.StreamReader
Poems.Capacity = 50 'Not quite 50 poems, so call
Poems.TrimToSize() before caching
For Each poemfile As String In poemfiles
poemstreamreader = System.IO.File.OpenText(poemfile)
Poems.Add(poemstreamreader.ReadLine(), poemfile)
poemstreamreader.Close()
Next
Poems.TrimToSize()
HttpContext.Current.Application.Lock()
HttpContext.Current.Application.Add("poemlist", Poems)
HttpContext.Current.Application.UnLock()
Return Poems
Else
Return CType(HttpContext.Current.Application("poemlist"),
SortedList)
End If
End Function
However, when I add, delete, or modify a file in the directory it does
not seem to delete the HttpApplicationState variable. Am I forgetting to do something? Am I doing something wrong? Any help would be appreciated, or possibly an example that mentions all the necessary & required steps. Thank you to anyone who can give me any help with this.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/



Apr 22 '06 #7

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

Similar topics

2
by: Jet Leung | last post by:
Hi all, I had made a program to watching files in my directory. I had used a instance of FileSystemWatcher to do my work.And I had add some events of the FileSystemWatcher , for example onChange,...
2
by: Steel City Phantom | last post by:
i am building a content distribution system using the filesystemwatcher to catch people moving files in and out of the system and update a database. what happens is when a process runs that moves...
3
by: Stampede | last post by:
Hi, I want to use the FileSystemWatcher in a Windows Service. I read an article, where the author created the FileSystemWatcher object in a seperate thread and when the event is fired, he started...
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...
2
by: kmcnet | last post by:
Hello Everyone and thanks for your help in advance. I have been battling a problem for nearly a month with the FileSystemWatcher component. Basically, what I am trying to do it to monitor three...
9
by: Tushar | last post by:
Followup-To: microsoft.public.dotnet.general Does anyone know when is this event raised, is it: 1) When the file is created but may not have been closed 2) When the file is created AND it has...
2
by: Steve | last post by:
I have a FileSystemWatcher watching a directory where I will add and update files. When it detects a change, I search through all the files and update my list of files in the UI. This works fine....
3
by: Nathan Sokalski | last post by:
I have a directory on my site that I keep a bunch of text files in (this directory is "/poetry/poems/"). The Application keeps the first line of each of these files in an HttpApplicationState...
5
by: =?Utf-8?B?Sm9obiBT?= | last post by:
I am trying to find out if there is a way to tell if there is already a filesystemwatcher (created by a webservice) monitoring a folder. I have a webservice that creates a filesystemwatcher,...
1
by: Lila Godel | last post by:
My VB.NET 2008 application is setup with a Sub Main and no forms. At run time a NotifyIcon is created with one context menu choice (Close which terminates app). I have no trouble running the...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.