0

我创建了一个服务,它的作用是将信息写入 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) 
4

2 回答 2

0

您的服务是否有可能会访问它两次,每次都试图获得一个排他锁?

于 2009-01-21T17:37:59.793 回答
0

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

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

于 2009-01-26T13:05:09.460 回答