473,473 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Windows Service and FileSystemWatcher Problems

I have made a simple console application that creates a webpage from my
playlist, then uploads it to my webhost. I decided to convert it to a
windows service and use the FileSystemWatcher to monitor the playlist file,
this is where i became stuck. I know the service detects changes to the
playlist as when running the Change event of FileSystemWatcher, I add an
item to the event log. I've tried making the service without including the
upload line (this is an external call to pscp.exe) but the html file isn't
made so im thinking there is a problem elsewhere in the program. Here is the
code for the FileSystemWatcher changed event as I think this is where the
error is:

private void FileMonitor_Changed(object sender,
System.IO.FileSystemEventArgs e)
{
eventLog1.WriteEntry("It Should Upload it here :|");//This entry appears
in the event log
System.Threading.Thread.Sleep(5000); //I tried adding a 5 second pause
to ensure the playlist file is saved
StreamReader input = new StreamReader(@"c:\my music\playlist.pls");
StreamWriter output = new StreamWriter("playlist.html"); //This file is
never created
String inputString;
int numSongs = 0;
int totalLength=0;
inputString = input.ReadLine();
ArrayList songList = new ArrayList();
while((inputString != null)||!(inputString.StartsWith("Number")))
{
inputString = input.ReadLine();
if(inputString.StartsWith("Number"))
{
break;
}
numSongs++;
inputString = input.ReadLine();
int equalPos = inputString.IndexOf("=")+1;
String title = inputString.Substring(equalPos);
equalPos = inputString.IndexOf("=")+2;
int length = int.Parse(input.ReadLine().Substring(equalPos));
totalLength += length;
int lengthSec = length%60;
int lengthMin = (length-lengthSec)/60;
String zeroed = "";
if(lengthSec<10)
{
zeroed = "0";
}
String song = numSongs+". "+title+"
("+lengthMin+":"+zeroed+lengthSec+")";
songList.Add(song);
}
int avLen = totalLength/numSongs;
int avLenSec = avLen%60;
int avLenMin = (avLen-avLenSec)/60;
String avLength = avLenMin+":"+avLenSec;
String time = "";
if(totalLength>=3600)
{
int numSecs = totalLength%60;
int numMins = ((totalLength-numSecs)%3600)/60;
int numHours = ((totalLength-numSecs)-(numMins*60))/3600;
time = numHours+"</font><font color=\"#409FFF\" face=\"Arial\">
hours </font><font face=\"Arial\" color=\"#FFBF00\">"+numMins+"</font><font
color=\"#409FFF\" face=\"Arial\"> minutes </font><font face=\"Arial\"
color=\"#FFBF00\">"+numSecs+"</font><font color=\"#409FFF\" face=\"Arial\">
seconds </font><BR>";
}
else
{
int numSecs = totalLength%60;
int numMins = ((totalLength-numSecs)%3600)/60;
time = numMins+"</font><font color=\"#409FFF\" face=\"Arial\">
minutes </font><font face=\"Arial\"
color=\"#FFBF00\">"+numSecs+"</font><font color=\"#409FFF\" face=\"Arial\">
seconds </font><BR>";
}
//The webpage is designed to look just like the Winamp Generated
Playlist
output.WriteLine("<html><head><link rel=\"stylesheet\" href=\"null\">");
output.WriteLine("<style TYPE=\"text/css\">\n<!--BODY { background:
#000040; }");
output.WriteLine(".para1 { margin-top: -42px; margin-left: 145px;
margin-right: 10px; font-family: \"font2, Arial\"; font-size: 30px;
line-height: 35px; text-align: left; color: #E1E1E1; }");
output.WriteLine(".para2 { margin-top: 15px; margin-left: 15px;
margin-right: 50px; font-family: \"font1, Arial Black\"; font-size: 50px;
line-height: 40px; text-align: left; color: #004080; }--></style>");
output.WriteLine("<title>Winamp Generated PlayList</title></head><body
BGCOLOR=\"#000080\" topmargin=\"0\" leftmargin=\"0\" text=\"#FFFFFF\">");
output.WriteLine("<div align=\"center\"><div CLASS=\"para2\"
align=\"center\"><p>WINAMP</p></div><div CLASS=\"para1\" align=\"center\">")
;
output.WriteLine("<p>playlist</p></div></div><hr align=\"left\"
width=\"90%\" noshade size=\"1\" color=\"#FFBF00\"><div align=\"right\">");
output.WriteLine("<table border=\"0\" cellspacing=\"0\"
cellpadding=\"0\" width=\"98%\"><tr><td><small><small><font face=\"Arial\"
color=\"#FFBF00\">");
output.WriteLine(numSongs+"</font><font color=\"#409FFF\"
face=\"Arial\"> tracks in playlist, average track length: </font><font
face=\"Arial\" color=\"#FFBF00\">"+avLength+"</font></small></small><br>");
output.WriteLine("<small><small><font color=\"#409FFF\"
face=\"Arial\">Playlist length: </font><font face=\"Arial\"
color=\"#FFBF00\">"+time);
output.WriteLine("</td></tr></table></div><blockquote><p><font
color=\"#FFBF00\" face=\"Arial\"><big>Playlist files:</big></font><ul><font
face=\"Arial\" color=\"#FFFFFF\"><small>");
for(int i=0; i<songList.Count;i++)
{
output.WriteLine(songList[i].ToString()+"<BR>");
}
output.WriteLine("</font></ul></blockquote><hr align=\"left\"
width=\"90%\" noshade size=\"1\" color=\"#FFBF00\"></body></html>");
input.Close();
output.Close();
System.Diagnostics.Process.Start("pscp.exe", " -pw password
playlist.html user@host:/file/location");
}

Any Help you can give would be appreciated.
Nov 16 '05 #1
2 2260
Hi Bonnett,

Why don;t you debug it?

Just use Debug/ Processes and attach it to the correct process, then put a
breakpoint in the first line of the method and see what's going on.
A warning, depending of the events that you are capturing you may get two
events risen , so you have to look into this, with the breakpoint it will
be clear though.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Bonnett" <Pe**@Pete-B.co.uk> wrote in message
news:2i************@uni-berlin.de...
I have made a simple console application that creates a webpage from my
playlist, then uploads it to my webhost. I decided to convert it to a
windows service and use the FileSystemWatcher to monitor the playlist file, this is where i became stuck. I know the service detects changes to the
playlist as when running the Change event of FileSystemWatcher, I add an
item to the event log. I've tried making the service without including the
upload line (this is an external call to pscp.exe) but the html file isn't
made so im thinking there is a problem elsewhere in the program. Here is the code for the FileSystemWatcher changed event as I think this is where the
error is:

private void FileMonitor_Changed(object sender,
System.IO.FileSystemEventArgs e)
{
eventLog1.WriteEntry("It Should Upload it here :|");//This entry appears in the event log
System.Threading.Thread.Sleep(5000); //I tried adding a 5 second pause
to ensure the playlist file is saved
StreamReader input = new StreamReader(@"c:\my music\playlist.pls");
StreamWriter output = new StreamWriter("playlist.html"); //This file is never created
String inputString;
int numSongs = 0;
int totalLength=0;
inputString = input.ReadLine();
ArrayList songList = new ArrayList();
while((inputString != null)||!(inputString.StartsWith("Number")))
{
inputString = input.ReadLine();
if(inputString.StartsWith("Number"))
{
break;
}
numSongs++;
inputString = input.ReadLine();
int equalPos = inputString.IndexOf("=")+1;
String title = inputString.Substring(equalPos);
equalPos = inputString.IndexOf("=")+2;
int length = int.Parse(input.ReadLine().Substring(equalPos));
totalLength += length;
int lengthSec = length%60;
int lengthMin = (length-lengthSec)/60;
String zeroed = "";
if(lengthSec<10)
{
zeroed = "0";
}
String song = numSongs+". "+title+"
("+lengthMin+":"+zeroed+lengthSec+")";
songList.Add(song);
}
int avLen = totalLength/numSongs;
int avLenSec = avLen%60;
int avLenMin = (avLen-avLenSec)/60;
String avLength = avLenMin+":"+avLenSec;
String time = "";
if(totalLength>=3600)
{
int numSecs = totalLength%60;
int numMins = ((totalLength-numSecs)%3600)/60;
int numHours = ((totalLength-numSecs)-(numMins*60))/3600;
time = numHours+"</font><font color=\"#409FFF\" face=\"Arial\">
hours </font><font face=\"Arial\" color=\"#FFBF00\">"+numMins+"</font><font color=\"#409FFF\" face=\"Arial\"> minutes </font><font face=\"Arial\"
color=\"#FFBF00\">"+numSecs+"</font><font color=\"#409FFF\" face=\"Arial\"> seconds </font><BR>";
}
else
{
int numSecs = totalLength%60;
int numMins = ((totalLength-numSecs)%3600)/60;
time = numMins+"</font><font color=\"#409FFF\" face=\"Arial\">
minutes </font><font face=\"Arial\"
color=\"#FFBF00\">"+numSecs+"</font><font color=\"#409FFF\" face=\"Arial\"> seconds </font><BR>";
}
//The webpage is designed to look just like the Winamp Generated
Playlist
output.WriteLine("<html><head><link rel=\"stylesheet\" href=\"null\">"); output.WriteLine("<style TYPE=\"text/css\">\n<!--BODY { background:
#000040; }");
output.WriteLine(".para1 { margin-top: -42px; margin-left: 145px;
margin-right: 10px; font-family: \"font2, Arial\"; font-size: 30px;
line-height: 35px; text-align: left; color: #E1E1E1; }");
output.WriteLine(".para2 { margin-top: 15px; margin-left: 15px;
margin-right: 50px; font-family: \"font1, Arial Black\"; font-size: 50px;
line-height: 40px; text-align: left; color: #004080; }--></style>");
output.WriteLine("<title>Winamp Generated PlayList</title></head><body
BGCOLOR=\"#000080\" topmargin=\"0\" leftmargin=\"0\" text=\"#FFFFFF\">");
output.WriteLine("<div align=\"center\"><div CLASS=\"para2\"
align=\"center\"><p>WINAMP</p></div><div CLASS=\"para1\" align=\"center\">") ;
output.WriteLine("<p>playlist</p></div></div><hr align=\"left\"
width=\"90%\" noshade size=\"1\" color=\"#FFBF00\"><div align=\"right\">"); output.WriteLine("<table border=\"0\" cellspacing=\"0\"
cellpadding=\"0\" width=\"98%\"><tr><td><small><small><font face=\"Arial\"
color=\"#FFBF00\">");
output.WriteLine(numSongs+"</font><font color=\"#409FFF\"
face=\"Arial\"> tracks in playlist, average track length: </font><font
face=\"Arial\" color=\"#FFBF00\">"+avLength+"</font></small></small><br>"); output.WriteLine("<small><small><font color=\"#409FFF\"
face=\"Arial\">Playlist length: </font><font face=\"Arial\"
color=\"#FFBF00\">"+time);
output.WriteLine("</td></tr></table></div><blockquote><p><font
color=\"#FFBF00\" face=\"Arial\"><big>Playlist files:</big></font><ul><font face=\"Arial\" color=\"#FFFFFF\"><small>");
for(int i=0; i<songList.Count;i++)
{
output.WriteLine(songList[i].ToString()+"<BR>");
}
output.WriteLine("</font></ul></blockquote><hr align=\"left\"
width=\"90%\" noshade size=\"1\" color=\"#FFBF00\"></body></html>");
input.Close();
output.Close();
System.Diagnostics.Process.Start("pscp.exe", " -pw password
playlist.html user@host:/file/location");
}

Any Help you can give would be appreciated.

Nov 16 '05 #2
Its fixed now, I added a timeout to ensure the file was saved, and altered
the service so instead of running as "system" it now runs as me.

Bonnett
Nov 16 '05 #3

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

Similar topics

0
by: Lloyd Sheen | last post by:
I have created a FileSystemWatcher application that monitors a set of folder for changes. This works well as a windows app. I have copied the code from the windows app to a windows service and...
1
by: Rich Miller | last post by:
Previously posted on the VB newsgroup with no replies, so any ideas appreciated. I have written a service application in VB (VS.Net 2003). On start, the service creates an object that...
3
by: Craig Thompson | last post by:
I've attempted to write a windows service that creates one FileSystemWatcher for each entry in a XML config file. Everything start perfrectly and runs as I expect for about 5 minutes and then...
3
by: Vinny | last post by:
Hey all. I'm now attributing the problem I'm having to something incredibly simple, to which I must be overlooking. I created a Windows Service application that listens for files. The file paths...
0
by: Rich Miller | last post by:
Any ideas appreciated. I have written a service application in VB (VS.Net 2003). On start, the service creates an object that instantiates a System.IO.FileSystemWatcher like so (the _watcher is...
2
by: Trevor | last post by:
Argh! This problem is driving me nuts! Can you help? In November of 2003, I installed a web service on Windows Server 2003 built in VB.NET for v1.1.4322 of the framework. It contains a timer...
6
by: Chris Marsh | last post by:
All I have a database table, changes to the data within which I am interested in acting on. The approach that I'm taking is to have the database update a file every time data is updated. This...
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,...
8
by: Ollie Riches | last post by:
I'm looking into a production issue related to a windows service and System.Timers.Timer. The background is the windows service uses a System.Timers.Timer to periodically poll a directory location...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.