I have a small problem with my windows service. The first time I start the service everything works the way it's suppose to, but randomly the service fails. The jist of the service is to use the filewatcher component to watch a directory. On create only, the service will read the small XML file, process the information. Once the file is finished processing the information, it should copy the file to another directory and delete the original.
After the first file is processed by the filewatcher, the following error message occurs on all the files after "The process cannot access the file "C:\test\rk.xml" because it is being used by another process".
Can someone tell me why the file is not being released? Any help would be greatly appreciated! Please see the code snippet below:
protected void fsw_Created (
object sender, System.IO.FileSystemEventArgs e)
{
XmlTextReader reader = new XmlTextReader (e.FullPath);
//Declare Array
String[] a = new String[10];
int arrayCounter = 0;
try
{
while (reader.Read())
{
switch (reader.NodeType)
{
// case XmlNodeType.Element: // The node is an element.
// Console.Write("<" + reader.Name);
// Console.WriteLine(">");
// break;
case XmlNodeType.Text: //Display the text in each element.
// Console.WriteLine (reader.Value);
a[arrayCounter] = reader.Value;
arrayCounter += 1;
break;
// case XmlNodeType.EndElement: //Display the end of the element.
// Console.Write("</" + reader.Name);
// Console.WriteLine(">");
// break;
//End of switch
}
//End of while
}
if (reader.EOF == true)
{
reader.Close();
}
string prcinstance = a[0];
string[] actionNames = new string[a.Length -1];
Array.Copy(a,1,actionNames,0,9);
//File.Delete(e.FullPath);
File.Copy(e.FullPath,"c:\\" + e.Name,true);
EventLog.WriteEntry("Monitor", e.Name + " has been copied");
if (File.Exists("c:\\" + e.Name))
{
File.Delete(e.FullPath);
EventLog.WriteEntry("Monitor", e.Name + " has been copied");
}
}
catch (Exception ex)
{
EventLog.WriteEntry("Monitor - Error", ex.Message);
}
}
Thanks,
Ron 4 1799
Perhaps you're getting an exception and you're not closing the reader? Maybe
using a finally block and closing the reader there would help.
i.e.,
finally
{
if (reader != null)
reader.Close();
}
Also I didn't look too closely at your code but this:
if (reader.EOF)
{
reader.Close();
}
seems kind of iffy? Why not just exit the loop and close it? It seems kind
of pointless to check for EOF and then close.
--
____________________
Klaus H. Probst, MVP http://www.vbbox.com/
"Ron King Jr" <ro*******@hotmail.com> wrote in message
news:25**********************************@microsof t.com... I have a small problem with my windows service. The first time I start the
service everything works the way it's suppose to, but randomly the service
fails. The jist of the service is to use the filewatcher component to watch
a directory. On create only, the service will read the small XML file,
process the information. Once the file is finished processing the
information, it should copy the file to another directory and delete the
original. After the first file is processed by the filewatcher, the following error
message occurs on all the files after "The process cannot access the file
"C:\test\rk.xml" because it is being used by another process". Can someone tell me why the file is not being released? Any help would be
greatly appreciated! Please see the code snippet below: protected void fsw_Created ( object sender, System.IO.FileSystemEventArgs e) {
XmlTextReader reader = new XmlTextReader (e.FullPath);
//Declare Array String[] a = new String[10]; int arrayCounter = 0;
try {
while (reader.Read()) {
switch (reader.NodeType) { // case XmlNodeType.Element: // The node is an element. // Console.Write("<" + reader.Name); // Console.WriteLine(">"); // break; case XmlNodeType.Text: //Display the text in each element. // Console.WriteLine (reader.Value); a[arrayCounter] = reader.Value; arrayCounter += 1; break; // case XmlNodeType.EndElement: //Display the end of the element. // Console.Write("</" + reader.Name); // Console.WriteLine(">"); // break;
//End of switch }
//End of while }
if (reader.EOF == true)
{ reader.Close();
} string prcinstance = a[0]; string[] actionNames = new string[a.Length -1]; Array.Copy(a,1,actionNames,0,9); //File.Delete(e.FullPath); File.Copy(e.FullPath,"c:\\" + e.Name,true); EventLog.WriteEntry("Monitor", e.Name + " has been copied"); if (File.Exists("c:\\" + e.Name))
{
File.Delete(e.FullPath);
EventLog.WriteEntry("Monitor", e.Name + " has been copied");
}
}
catch (Exception ex) { EventLog.WriteEntry("Monitor - Error", ex.Message);
} }
Thanks, Ron
Thanks for the repsonse Klaus
I added the finally statement to the code and removed the reader.EOF check. The service is running better, but I still randomly get the error with a process holding on the file. We I drop new files with different names sometimes it processes. Any other advice you can share
finall
if (reader != null
reader.Close()
File.Move(e.FullPath,"c:\\" + e.Name)
Thanks again!
I have a service with FileWatcher and had to implement the dealing of files
to a timer. When I drag/drop a file it sends about 4-5 events so it seems
that when the first event fires and I try to deal with the file there is
still activity on the file. I get around this by using a timer (not a
forms.timer).
Lloyd Sheen
"Ron King Jr" <ro*******@hotmail.com> wrote in message
news:BD**********************************@microsof t.com... Thanks for the repsonse Klaus!
I added the finally statement to the code and removed the reader.EOF
check. The service is running better, but I still randomly get the error
with a process holding on the file. We I drop new files with different
names sometimes it processes. Any other advice you can share? finally {
if (reader != null) { reader.Close();
} File.Move(e.FullPath,"c:\\" + e.Name); }
Thanks again!
Lloyd
Is it psssible to provide some code snippet for the placement of the timer? Also, how long did your timer wait before processing again
Thanks for your help! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andy Neilson |
last post by:
I've run across a strange behaviour with XmlSerializer that I'm unable to explain. I came across this while trying to use XmlSerializer to deserialize from a the details of a SoapException. This...
|
by: SHC |
last post by:
Hi all,
I did the "Build" on the attached code in my VC++ .NET 2003 - Windows XP Pro
PC. On the c:\ screen, I got the following: Microsoft Development Environment
An unhandled exception of type...
|
by: Troy Murphy |
last post by:
The help file for Visual Studio .NET version 1 had a Walkthru for installing
a Windows Service that would monitor a folder.
Version 1.1 does not seem to have that example anymore.
Could someone...
|
by: adam_scheich |
last post by:
When creating a file in excel and saving it to a directory that is
being watched by filewatcher using Save As, the filewatcher created
event doesn't fire.
It works fine for text files, but for...
|
by: Roger Twomey |
last post by:
I am working on a filewatcher application.
The premis is:
User uploads an xml file onto the web server
the filewatcher app sees the xml file
filewatcher app reads the file and inserts...
|
by: Ron King Jr |
last post by:
I have a small problem with my windows service. The first time I start the service everything works the way it's suppose to, but randomly the service fails. The jist of the service is to use the...
|
by: ba.hons |
last post by:
Hello,
I have a file watcher object which i use to check a directory for XML
files.
When an XML file is placed in the directory i check the filename, to
make sure it for me, then parse the...
|
by: tshad |
last post by:
I have a filewatcher program that is working syncronously (which is how I
want it to work).
But my program will handle whatever files are in the folder at the time it
runs. So when I drop 15...
|
by: tshad |
last post by:
What exactly is FileWatcher doing? When you drop 100 files in a folder it
is watching, it normally will fire of the event 100 times.
In my case, I do all my processing on the first event so I...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |