473,721 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FileSystemWatch er 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 HttpApplication State variable as a SortedList. When I
add or modify a file in this directory, I want to delete this
HttpApplication State variable. I tried to do this using the following lines
of code in Global.asax.vb:
Private WithEvents poemfilewatcher As New
IO.FileSystemWa tcher(HttpConte xt.Current.Serv er.MapPath("/poetry/poems/"))

Private Sub PoemDirModified (ByVal sender As Object, ByVal e As
System.IO.FileS ystemEventArgs) Handles poemfilewatcher .Changed,
poemfilewatcher .Created, poemfilewatcher .Deleted
HttpContext.Cur rent.Applicatio n.Lock()
HttpContext.Cur rent.Applicatio n.Remove("poeml ist")
HttpContext.Cur rent.Applicatio n.UnLock()
End Sub

Sub Application_Sta rt(ByVal sender As Object, ByVal e As EventArgs)
poemfilewatcher .IncludeSubdire ctories = True
poemfilewatcher .EnableRaisingE vents = True
End Sub
The Function that I use to return this SortedList, whether it is stored in
an HttpApplication State 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.Cur rent.Applicatio n("poemlist") Is Nothing Then
Dim Poems As New SortedList(New CaseInsensitive Comparer)
Dim poemfiles As String() =
System.IO.Direc tory.GetFiles(H ttpContext.Curr ent.Server.MapP ath("/poetry/poems/"))
Dim poemstreamreade r As System.IO.Strea mReader
Poems.Capacity = 50 'Not quite 50 poems, so call Poems.TrimToSiz e()
before caching
For Each poemfile As String In poemfiles
poemstreamreade r = System.IO.File. OpenText(poemfi le)
Poems.Add(poems treamreader.Rea dLine(), poemfile)
poemstreamreade r.Close()
Next
Poems.TrimToSiz e()
HttpContext.Cur rent.Applicatio n.Lock()
HttpContext.Cur rent.Applicatio n.Add("poemlist ", Poems)
HttpContext.Cur rent.Applicatio n.UnLock()
Return Poems
Else
Return CType(HttpConte xt.Current.Appl ication("poemli st"),
SortedList)
End If
End Function
However, when I add, delete, or modify a file in the directory it does not
seem to delete the HttpApplication State 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********@hotm ail.com
http://www.nathansokalski.com/
Apr 21 '06 #1
6 1365
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.Cur rent. 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********@hot mail.com> wrote in message
news:uK******** ******@TK2MSFTN GP05.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 HttpApplication State variable as a SortedList.
When I add or modify a file in this directory, I want to delete this
HttpApplicatio nState variable. I tried to do this using the following lines
of code in Global.asax.vb:
Private WithEvents poemfilewatcher As New
IO.FileSystemWa tcher(HttpConte xt.Current.Serv er.MapPath("/poetry/poems/"))

Private Sub PoemDirModified (ByVal sender As Object, ByVal e As
System.IO.FileS ystemEventArgs) Handles poemfilewatcher .Changed,
poemfilewatcher .Created, poemfilewatcher .Deleted
HttpContext.Cur rent.Applicatio n.Lock()
HttpContext.Cur rent.Applicatio n.Remove("poeml ist")
HttpContext.Cur rent.Applicatio n.UnLock()
End Sub

Sub Application_Sta rt(ByVal sender As Object, ByVal e As EventArgs)
poemfilewatcher .IncludeSubdire ctories = True
poemfilewatcher .EnableRaisingE vents = True
End Sub
The Function that I use to return this SortedList, whether it is stored in
an HttpApplication State 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.Cur rent.Applicatio n("poemlist") Is Nothing Then
Dim Poems As New SortedList(New CaseInsensitive Comparer)
Dim poemfiles As String() =
System.IO.Direc tory.GetFiles(H ttpContext.Curr ent.Server.MapP ath("/poetry/poems/"))
Dim poemstreamreade r As System.IO.Strea mReader
Poems.Capacity = 50 'Not quite 50 poems, so call Poems.TrimToSiz e()
before caching
For Each poemfile As String In poemfiles
poemstreamreade r = System.IO.File. OpenText(poemfi le)
Poems.Add(poems treamreader.Rea dLine(), poemfile)
poemstreamreade r.Close()
Next
Poems.TrimToSiz e()
HttpContext.Cur rent.Applicatio n.Lock()
HttpContext.Cur rent.Applicatio n.Add("poemlist ", Poems)
HttpContext.Cur rent.Applicatio n.UnLock()
Return Poems
Else
Return CType(HttpConte xt.Current.Appl ication("poemli st"),
SortedList)
End If
End Function
However, when I add, delete, or modify a file in the directory it does not
seem to delete the HttpApplication State 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********@hotm ail.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********@hotm ail.com
http://www.nathansokalski.com/

"Peter Johnson" <pe***********@ gmail.com> wrote in message
news:11******** **************@ z34g2000cwc.goo glegroups.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********@hotm ail.com
http://www.nathansokalski.com/

"bruce barker (sqlwork.com)" <b_************ *************@s qlwork.com> wrote
in message news:%2******** ********@TK2MSF TNGP02.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.Cur rent. 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********@hot mail.com> wrote in message
news:uK******** ******@TK2MSFTN GP05.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 HttpApplication State variable as a SortedList.
When I add or modify a file in this directory, I want to delete this
HttpApplicati onState variable. I tried to do this using the following
lines of code in Global.asax.vb:
Private WithEvents poemfilewatcher As New
IO.FileSystemWa tcher(HttpConte xt.Current.Serv er.MapPath("/poetry/poems/"))

Private Sub PoemDirModified (ByVal sender As Object, ByVal e As
System.IO.FileS ystemEventArgs) Handles poemfilewatcher .Changed,
poemfilewatcher .Created, poemfilewatcher .Deleted
HttpContext.Cur rent.Applicatio n.Lock()
HttpContext.Cur rent.Applicatio n.Remove("poeml ist")
HttpContext.Cur rent.Applicatio n.UnLock()
End Sub

Sub Application_Sta rt(ByVal sender As Object, ByVal e As EventArgs)
poemfilewatcher .IncludeSubdire ctories = True
poemfilewatcher .EnableRaisingE vents = True
End Sub
The Function that I use to return this SortedList, whether it is stored
in an HttpApplication State 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.Cur rent.Applicatio n("poemlist") Is Nothing Then
Dim Poems As New SortedList(New CaseInsensitive Comparer)
Dim poemfiles As String() =
System.IO.Direc tory.GetFiles(H ttpContext.Curr ent.Server.MapP ath("/poetry/poems/"))
Dim poemstreamreade r As System.IO.Strea mReader
Poems.Capacity = 50 'Not quite 50 poems, so call
Poems.TrimToSiz e() before caching
For Each poemfile As String In poemfiles
poemstreamreade r = System.IO.File. OpenText(poemfi le)
Poems.Add(poems treamreader.Rea dLine(), poemfile)
poemstreamreade r.Close()
Next
Poems.TrimToSiz e()
HttpContext.Cur rent.Applicatio n.Lock()
HttpContext.Cur rent.Applicatio n.Add("poemlist ", Poems)
HttpContext.Cur rent.Applicatio n.UnLock()
Return Poems
Else
Return CType(HttpConte xt.Current.Appl ication("poemli st"),
SortedList)
End If
End Function
However, when I add, delete, or modify a file in the directory it does
not seem to delete the HttpApplication State 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********@hotm ail.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.publi c.dotnet.framew ork.adonet,
microsoft.publi c.dotnet.framew ork.aspnet,
microsoft.publi c.dotnet.framew ork.aspnet.buil dingcontrols,
microsoft.publi c.dotnet.framew ork.aspnet.webc ontrols,
microsoft.publi c.dotnet.genera l,
microsoft.publi c.dotnet.l -> Apparently, you've reached some sort of limit
here

Bob Lehmann

"Nathan Sokalski" <nj********@hot mail.com> wrote in message
news:ug******** ******@TK2MSFTN GP05.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********@hotm ail.com
http://www.nathansokalski.com/

"bruce barker (sqlwork.com)" <b_************ *************@s qlwork.com> wrote in message news:%2******** ********@TK2MSF TNGP02.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.Cur rent. 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********@hot mail.com> wrote in message
news:uK******** ******@TK2MSFTN GP05.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 HttpApplication State variable as a SortedList.
When I add or modify a file in this directory, I want to delete this
HttpApplicati onState variable. I tried to do this using the following
lines of code in Global.asax.vb:
Private WithEvents poemfilewatcher As New
IO.FileSystemWa tcher(HttpConte xt.Current.Serv er.MapPath("/poetry/poems/"))
Private Sub PoemDirModified (ByVal sender As Object, ByVal e As
System.IO.FileS ystemEventArgs) Handles poemfilewatcher .Changed,
poemfilewatcher .Created, poemfilewatcher .Deleted
HttpContext.Cur rent.Applicatio n.Lock()
HttpContext.Cur rent.Applicatio n.Remove("poeml ist")
HttpContext.Cur rent.Applicatio n.UnLock()
End Sub

Sub Application_Sta rt(ByVal sender As Object, ByVal e As EventArgs)
poemfilewatcher .IncludeSubdire ctories = True
poemfilewatcher .EnableRaisingE vents = True
End Sub
The Function that I use to return this SortedList, whether it is stored
in an HttpApplication State 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.Cur rent.Applicatio n("poemlist") Is Nothing Then
Dim Poems As New SortedList(New CaseInsensitive Comparer)
Dim poemfiles As String() =
System.IO.Direc tory.GetFiles(H ttpContext.Curr ent.Server.MapP ath("/poetry/poe
ms/")) Dim poemstreamreade r As System.IO.Strea mReader
Poems.Capacity = 50 'Not quite 50 poems, so call
Poems.TrimToSiz e() before caching
For Each poemfile As String In poemfiles
poemstreamreade r = System.IO.File. OpenText(poemfi le)
Poems.Add(poems treamreader.Rea dLine(), poemfile)
poemstreamreade r.Close()
Next
Poems.TrimToSiz e()
HttpContext.Cur rent.Applicatio n.Lock()
HttpContext.Cur rent.Applicatio n.Add("poemlist ", Poems)
HttpContext.Cur rent.Applicatio n.UnLock()
Return Poems
Else
Return CType(HttpConte xt.Current.Appl ication("poemli st"),
SortedList)
End If
End Function
However, when I add, delete, or modify a file in the directory it does
not seem to delete the HttpApplication State 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********@hotm ail.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
2225
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, onRename and so on. And I had made this program as a windows service.As I know, if I delete a file from my directory, this behavior will active this program and do something what I want to do. But after I install this program as a windows service...
2
5481
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 hundreds of files (happens every day) i get all kinds of debug errors and an unhandled exception. here is the debug log First-chance exception at 0x7c342eee (msvcr71.dll) in VoiceScanner.exe: 0xC0000005: Access violation reading location...
3
14304
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 a working thread for processing the file, created a new FileSystemWatcher (as he said for real time processing), and then called the join method for the first thread. I can't really see the sence in this. Aren't the events of the...
20
4491
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 logic layer(so far so good and easy).I initialize my class which is using a FileSystemWatcher in my Global.asax and everything works fine.I have found FileSystemWatcher class not very reliable and sometimes it behavies unexpectedly.I'm afriad that...
2
1994
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 separate folders on a Windows 2000 server for activity. In my code, I have set up three separate subroutines: FaxWatch() ReportWatch() ScheduleWatch() Each of the subroutines creates a new instance of the FileSystemWatcher.
9
7126
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 been closed I am using this control in a Windows-Service I've developed. It works hoever the problem I'm having is that the file does not seem to be available when I receive this event. If I include a delay of a few seconds using Sleep(3000) for...
2
2404
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. Now, my other application that is writing the files that are being watched, does something like this: 1) Creates a file, a.txt 2) wants to name it b.txt but checks first if b.txt exists, if it does, it delete it 3) moves a.txt to b.txt...
3
1813
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 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...
5
5601
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, monitors a folder and then returns the contents of the new/changed files. However, if the client app loses connection to the webservice without closing the filewatcher, and then reconnects (and thus creates a new watcher), I believe I end up with...
1
3159
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 application and when I select Close from the icon's right click menu the system tray icon goes away and the application is removed from the listing on the processes tab of task manager. My problem is that the FileSystemWatcher event does not work....
0
9367
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9215
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9064
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8007
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6669
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4484
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3189
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2130
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.