问题
In my winphone app I have a strange situation where I get a System.IO.FileNotFoundException in code that does nothing related to files.
I have a class that manages delayed function calls:
// 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();
}
}
}
And a class that manages ProgressIndicator state:
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);
}
}
}
When I try to run set(true) method in code I get the following exception:
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
Why is this happening and how can this be fixed?
回答1:
Looks like your actual problem is permissions, the FileNotFound exception is probably happening as a result of not being able to read the file location directory.
I don't have any experience with Windows Phone development, however, I assume wherever System.Windows.ni.dll is resided requires higher permissions than what you are running your app under.
Update
Based on your call stack error message - "Invalid cross-thread access", the problem is your attempting to access/update a GUI component from another thread which isn't the UI thread. Try changing your code to:
Deployment.Current.Dispatcher.BeginInvoke(()=>
{
Progressor.IsIndeterminate = Progressor.IsVisible = isOn;
if (isOn)
At.Do(delegate { this.set(false); }, 1000);
}
});
来源:https://stackoverflow.com/questions/13972669/system-io-filenotfoundexception-in-delayed-function