我有一个计时器函数,它每分钟将对象序列化为xml文件一次。它会工作一段时间,但大约在38小时后,它会出现下面的IO异常。

我可以看到目录在那里,正如你在我的屏幕截图中看到的那样。此外,当这个问题发生时,我甚至可以在dos中输入以下命令: echo "test“> test.xml,我可以在该目录位置创建test.xml文件。
我的代码如下:
try
{
if (!Directory.Exists(filePath))
this.log.Write(LogLevel.Fatal, this.componentName, "Directory not exists: " + filePath);
lock (lockObject)
{
filepath = filepath + "ApplicationState.xml";
if (File.Exists(filePath))
File.Delete(filePath);
if (this.StateObject != null && !File.Exists(filePath))
{
using (Stream sWrite = File.Create(filePath))
{
this.Serializer = new XmlSerializer(typeof(State));
this.Serializer.Serialize(sWrite, this.StateObject);
}
}
}
}catch (Exception ex)
{
if (this.log != null)
{
this.log.Write(ex, this.componentName);
}
}
finally
{
if (this.StreamWriter != null)
{
this.StreamWriter.Close();
}
bRun = true;
}我尽了最大努力检查代码,以确保没有任何挂起的IO资源,老实说,在这一点上我相当迷茫……除了我用来打开和读取文件的IO之外,window CE或C#是否在常规IO上有某种类型的锁资源?
发布于 2012-09-11 02:08:16
我看到您正在写入名为"\Hard Disk“的东西,这使我认为您可能正在写入某个永久存储卷(例如,板载闪存、USB磁盘、CF卡或其他任何东西)。请记住,这些外围设备需要设备驱动程序,可能还需要一些中断处理程序,这些程序是由设备OEM提供的,而不是Microsoft作为操作系统的一部分。
总有可能是驱动链中某个地方的bug导致了这个问题--它看起来像是并发锁定失败。请记住,在使用嵌入式操作系统时,永远不要假设您看到的失败总是您的代码。OEM很容易在他们的代码中有bug。
我会用两种方式攻击它:
https://stackoverflow.com/questions/12354967
复制相似问题