2012-12-20 80 views
1

在我的winphone應用程序中,我有一個奇怪的情況,我得到System.IO.FileNotFoundException代碼中沒有任何與文件相關的代碼。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 

爲什麼會發生這種情況,怎麼可能這是固定的?

+0

如果您只是在調試器中看到異常(但不會中斷執行),那麼不用擔心,它只是Framework解析程序集的方式。 –

+3

堆棧跟蹤在解決這樣的問題時總是有用的。 –

+0

@Adriano,它不僅僅在調試器中,ProgressIndicator實際上並沒有消散,所以程序無法正常工作。 – Anton

回答

5

看起來您的實際問題是權限,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); 
} 

}); 
+0

好的,那麼如何解決這些權限問題以及它們爲什麼會發生? – Anton

+0

「*如何解決這些權限問題*」 - 嘗試給予您正在運行應用程序的讀取權限下的任何帳戶「System.Windows.ni.dll」所在的任何目錄。 – James

+0

它做了訣竅,謝謝。也許你可以推薦一些關於這個主題的文章,因爲坦率地說,我不知道爲什麼這段代碼有效,以及我應該如何解決類似的情況。 – Anton