MonoTouch File.ReadAllText在内部FileStream.Dispose()上抛出异常

时间:2012-03-06 18:28:47

标签: c# ios mono xamarin.ios

以下代码似乎偶尔会产生这种堆栈跟踪。在内部FileStream对象上调用dispose方法时,它看起来是在框架内发生的吗?对我来说似乎是一个错误,任何想法?

var previousVersion = long.Parse(File.ReadAllText(Paths.VersionFile));

堆栈跟踪:

 System.IO.IOException: Invalid parameter
      at System.IO.FileStream.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0 
      at System.IO.Stream.Close () [0x00000] in <filename unknown>:0 
      at System.IO.StreamReader.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0 
      at System.IO.TextReader.Dispose () [0x00000] in <filename unknown>:0 
      at System.IO.File.ReadAllText (System.String path) [0x00000] in <filename unknown>:0 
      at CatalystHD.Shared.BaseLoginController.CheckIfFirstRunThisVersion () [0x00000] in <filename unknown>:0 
      at CatalystHD.Shared.BaseLoginController.ViewDidLoad () [0x00000] in <filename unknown>:0 
      at MonoTouch.UIKit.UIViewController.get_View () [0x00000] in <filename unknown>:0 
      at CatalystHD.IPad.RootViewController.AnimateTo (MonoTouch.UIKit.UIViewController aController, UIViewAnimationTransition transition) [0x00000] in <filename unknown>:0 
      at CatalystHD.IPad.RootViewController.Logout (Boolean timeout) [0x00000] in <filename unknown>:0 
      at CatalystHD.IPad.NotebookSelectionController.logoutButton_Clicked (System.Object sender, System.EventArgs e) [0x00000] in <filename unknown>:0 
      at MonoTouch.UIKit.UIBarButtonItem Callback.Call (MonoTouch.Foundation.NSObject sender) [0x00000] in <filename unknown>:0 
      at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00000] in <filename unknown>:0 
      at MonoTouch.UIKit.UIApplication.Main (System.String[] args) [0x00000] in <filename unknown>:0 
      at CatalystHD.Application.Main (System.String[] args) [0x00000] in <filename unknown>:0 

修改 VersionFile路径就是这样抓住的:

public static string VersionFile {
    get {
        var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        return Path.Combine(path, "version.dat");
    }
}

1 个答案:

答案 0 :(得分:0)

如果我不得不猜测,我认为你可能会遇到竞争条件,因为在Flush()处理引擎盖FileStream期间发生隐式ReadAllText打电话。

(警告:不在单声道源之前,因此无法确认确切的行为)

是否有可能:

  • 您正在更换/重新开放/等。另一个线程中的文件路径相同吗?
  • 在另一个帖子之后或之后立即删除该路径?
  • 否则会失去对已检查路径的访问权限? (文件共享卸载?)