服务器上有非计划重启时,自定义服务无法启动?

时间:2009-01-21 12:17:24

标签: .net service startup

我创建了一个服务,它的作用是将信息写入xml文件。

有时,它似乎主要发生在计划外重启,服务无法启动,因为使用的文件是“锁定”。由于我的服务设置为“自动启动”,系统仍在启动时会发生此错误。

这是在Windows 2003下。

任何人都知道为什么会这样吗?

感谢。

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) 

2 个答案:

答案 0 :(得分:0)

您的服务是否有可能两次访问此服务,每次尝试获取一次独占锁定?

答案 1 :(得分:0)

如果问题是由重启后过早启动服务引起的,为什么不检查上次重启时间并跳过运行任务,直到重启后这么多分钟。 System.Environment.TickCount将为您提供自上次启动时间以来的毫秒数。

http://msdn.microsoft.com/en-us/library/system.environment.tickcount.aspx

相关问题