Home All Groups Group Topic Archive Search About

FileSystemWatcher does not seem to be working

Author
21 Apr 2006 11:15 PM
Nathan Sokalski
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.MapPath("/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.Server.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
njsokal***@hotmail.com
http://www.nathansokalski.com/

Author
22 Apr 2006 12:03 AM
bruce barker (sqlwork.com)
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)


Show quote
"Nathan Sokalski" <njsokal***@hotmail.com> wrote in message
news:uK8sGmZZGHA.4836@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.MapPath("/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.Server.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
> njsokal***@hotmail.com
> http://www.nathansokalski.com/
>
Author
22 Apr 2006 1:05 AM
Nathan Sokalski
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
njsokal***@hotmail.com
http://www.nathansokalski.com/

Show quote
"bruce barker (sqlwork.com)" <b_r_u_c_e_removeundersco***@sqlwork.com> wrote
in message news:%23WbQrAaZGHA.4752@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" <njsokal***@hotmail.com> wrote in message
> news:uK8sGmZZGHA.4836@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.MapPath("/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.Server.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
>> njsokal***@hotmail.com
>> http://www.nathansokalski.com/
>>
>
>
Author
22 Apr 2006 1:37 AM
Bob Lehmann
> 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.buildingcontrols,
microsoft.public.dotnet.framework.aspnet.webcontrols,
microsoft.public.dotnet.general,
microsoft.public.dotnet.l -> Apparently, you've reached some sort of limit
here

Bob Lehmann

Show quote
"Nathan Sokalski" <njsokal***@hotmail.com> wrote in message
news:ug9WfjaZGHA.4424@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
> njsokal***@hotmail.com
> http://www.nathansokalski.com/
>
> "bruce barker (sqlwork.com)" <b_r_u_c_e_removeundersco***@sqlwork.com>
wrote
> in message news:%23WbQrAaZGHA.4752@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" <njsokal***@hotmail.com> wrote in message
> > news:uK8sGmZZGHA.4836@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.MapPath("/poetry/poems/"))
Show quote
> >>
> >> 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.Server.MapPath("/poetry/poe
ms/"))
Show quote
> >>        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
> >> njsokal***@hotmail.com
> >> http://www.nathansokalski.com/
> >>
> >
> >
>
>
Author
22 Apr 2006 4:39 PM
Otis Mukinfus
Show quote
On Fri, 21 Apr 2006 19:37:49 -0600, "Bob Lehmann" <nospam@dontbotherme.zzz>
wrote:

>> 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.buildingcontrols,
>microsoft.public.dotnet.framework.aspnet.webcontrols,
>microsoft.public.dotnet.general,
>microsoft.public.dotnet.l -> Apparently, you've reached some sort of limit
>here
>
>Bob Lehmann
>
>"Nathan Sokalski" <njsokal***@hotmail.com> wrote in message
>news:ug9WfjaZGHA.4424@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
>> njsokal***@hotmail.com
>> http://www.nathansokalski.com/
>>
>> "bruce barker (sqlwork.com)" <b_r_u_c_e_removeundersco***@sqlwork.com>
>wrote
>> in message news:%23WbQrAaZGHA.4752@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" <njsokal***@hotmail.com> wrote in message
>> > news:uK8sGmZZGHA.4836@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.MapPath("/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.Server.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
>> >> njsokal***@hotmail.com
>> >> http://www.nathansokalski.com/
>> >>
>> >
>> >
>>
>>
>
Good Lord!  What a long string of groups!  I think he should get the over
posting award for the week!

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com

AddThis Social Bookmark Button