custom service failed to start when there is a unscheduled reboot on the server?
Question
I created a service and what it does is writing information into a xml file.
Sometime, it seem to happen mostly with unscheduled reboot, the service cannot start because the file used is "locked". This error happen while the system is still starting up since my service is set to "auto start".
This is under windows 2003.
Anyone know why this is happening?
thanks.
The process cannot access the file 'D:\myService\eventlog.xml' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding)
at System.Data.DataTable.WriteXml(String fileName, XmlWriteMode mode, Boolean writeHierarchy)
at System.Data.DataTable.WriteXml(String fileName)
at myService.c_control.addXmlLog(eventStatus timeStatus)
Solution
If the problem is caused by the service starting too early after a reboot, why not check to see the last reboot time and skip running the task until so many minutes after reboot. System.Environment.TickCount will give you the number of milliseconds since the last boot time.
http://msdn.microsoft.com/en-us/library/system.environment.tickcount.aspx
OTHER TIPS
Is it possible that your service could be accessing this twice, trying to get an exclusive lock on it each time?