延迟函数中的filenotfoundexception
本文关键字:filenotfoundexception 函数 延迟 | 更新日期: 2023-09-27 17:49:14
在我的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
驻留在哪里,都需要比您运行应用程序更高的权限。
基于你的调用堆栈错误消息-"无效的跨线程访问",问题是你试图从另一个线程访问/更新GUI组件,而不是UI线程。尝试将代码更改为:
Deployment.Current.Dispatcher.BeginInvoke(()=>
{
Progressor.IsIndeterminate = Progressor.IsVisible = isOn;
if (isOn)
At.Do(delegate { this.set(false); }, 1000);
}
});