1

我有一个计时器函数,它每分钟将一个对象序列化为一个 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 资源,在这一点上,老实说,我很迷茫……window CE 或 C# 是否有某种类型的锁定资源除了我用来打开和读取文件的 IO 之外的通用 IO?

4

1 回答 1

4

I see that you're writing to something called "\Hard Disk" which makes me think you're likely writing to some persistent storage volume (e.g. on-board flash, a USB disk, CF card or whatever). Bear in mind that these peripherals require a device driver and likely some interrrupt handlers, which are provided by the device OEM and not Microsoft as part of the OS.

There's always the possibility that there's a bug in the driver chain somewhere that's causing the issue - it smells like a concurrency lock failure. Remember, when working with an embedded OS, never assume a failure you see is always your code. The OEM could easily have a bug in their code.

I'd attack this two ways:

  1. Report the issue to the OEM and see if they can repro/fix it
  2. 尝试将重试逻辑添加到您自己的代码中,以查找并处理此问题作为解决方法。
于 2012-09-10T18:08:16.467 回答