469,938 Members | 2,492 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,938 developers. It's quick & easy.

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 2098
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Lloyd Sheen | last post: by
3 posts views Thread by Craig Thompson | last post: by
3 posts views Thread by Vinny | last post: by
2 posts views Thread by Trevor | last post: by
6 posts views Thread by Chris Marsh | last post: by
5 posts views Thread by =?Utf-8?B?Sm9obiBT?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.