c# - 延迟函数中的 System.IO.FileNotFoundException

标签 c# exception timer timeout windows-phone-8

在我的 winphone 应用程序中,我遇到了一个奇怪的情况,我在代码中得到了一个与文件无关的 System.IO.FileNotFoundException

我有一个管理延迟函数调用的类:

// Delayed function manager
namespace Test
{
    public static class At
    {
        private readonly static TimerCallback timer = 
            new TimerCallback(At.ExecuteDelayedAction);

        public static void Do(Action action, TimeSpan delay,
            int interval = Timeout.Infinite)
        {
            var secs = Convert.ToInt32(delay.TotalMilliseconds);
            new Timer(timer, action, secs, interval);
        }

        public static void Do(Action action, int delay, 
            int interval = Timeout.Infinite)
        {
            Do(action, TimeSpan.FromMilliseconds(delay), interval);
        }

        public static void Do(Action action, DateTime dueTime, 
            int interval = Timeout.Infinite)
        {
            if (dueTime < DateTime.Now) return;
            else Do(action, dueTime - DateTime.Now, interval);
        }

        private static void ExecuteDelayedAction(object o)
        {
            (o as Action).Invoke();
        }
    }
}

还有一个管理 ProgressIndicator 状态的类:

namespace Test
{
    public class Indicator
    {
        public DependencyObject ThePage;
        public ProgressIndicator Progressor;
        public Indicator(DependencyObject page)
        {
            ThePage = page;
            Progressor = new ProgressIndicator();
            SystemTray.SetProgressIndicator(ThePage, Progressor);
        }

        // If set(true) then set(false) in one second to remove ProgressIndicator
        public void set(bool isOn)
        {
            Progressor.IsIndeterminate = Progressor.IsVisible = isOn; // Exception happens on this line
            if (isOn) At.Do(delegate { this.set(false); }, 1000);
        }
    }
}

当我尝试在代码中运行 set(true) 方法时,出现以下异常:

An exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary
A first chance exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll

为什么会发生这种情况,如何解决?

最佳答案

看起来您的实际问题是权限,FileNotFound 异常可能是由于无法读取文件位置目录而发生的。

我没有任何 Windows Phone 开发经验,但是,我假设 System.Windows.ni.dll 所在的位置需要比您运行应用程序的权限更高的权限。

更新

根据您的调用堆栈错误消息 - “无效的跨线程访问”,问题是您试图从另一个非 UI 线程的线程访问/更新 GUI 组件。尝试将您的代码更改为:

Deployment.Current.Dispatcher.BeginInvoke(()=>
{ 
    Progressor.IsIndeterminate = Progressor.IsVisible = isOn;
    if (isOn) 
        At.Do(delegate { this.set(false); }, 1000);
}

});

关于c# - 延迟函数中的 System.IO.FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13972669/

相关文章:

java - 为什么运行时异常在Java代码中被称为运行时异常?

go - golang 中的可选超时

c# - 在 FindAll 函数中使用 List -C# LinQ

javascript - Angular7 等同于 C# 属性装饰器

c# - Generics Fun : Where typeof(List<T>) ! = typeof(List<T>),并使用反射获取泛型方法,带有泛型参数

java - 为什么带有 catch Exception 的空 try block 会编译?

python - 如何捕获多线程的异常?

c# - 获得唯一的机器ID

c# - System.Threading.Timer 一段时间后没有触发

python - 我可以在 Python 中同时运行多个计时器吗?