2013-04-30 41 views
0

我正在開發一個Windows窗體啓動器/更新/啓動畫面應用程序,用於C#中的Windows 7松下Toughbook(平板電腦)。發射器在我的桌面和我的Toughbooks之一上工作正常......但是,在「真實環境」設備上進行測試需要20多秒才能在首次啓動後顯示該表單。 (一旦顯示錶格,它就會很快,應用程序假設可以工作。)安裝後首次在設備上啓動很快,但首次啓動後需要很長時間。C#Windows窗體永遠加載

我不確定如何去測試,因爲這是一個無法通過USB連接到設備或放在我們的網絡上的設備。應用程序是否打開文件似乎並不重要。事實上,在大多數情況下,應用程序只是開放,看到現在是檢查另一個更新,啓動主應用程序,並殺死自己爲時過早。

當前過程看起來像這樣:

構造:

public MyConstructor() { InitializeComponent(); } 

形式負載:

private void Form1_Load(object sender, EventArgs e) 
{ 
    if (!isLoaded) 
    { 
     System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap("parentApplication.exe.config"); //Path to your config file 
     System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap); 
     log4net.Config.XmlConfigurator.Configure(); 
     WebRequest.url = configManager.AppSettings.Settings["SURL"].Value; 

     // Set a timer to run the update process momentarily to allow the UI thread to update and display. (UI must be idle to run Timer) 
     updateTimer = new System.Timers.Timer(1000); 
     updateTimer.Elapsed += new ElapsedEventHandler(beginUpdate); 
     updateTimer.Enabled = true; 
     isLoaded = true; 
    } 
} 

更新過程

private void beginUpdate(Object sender, EventArgs e) 
{ 
    // If any of this fails, we still want to launch the main application. 
    try 
    { 
      // Prevent the timer from doing anything else. 
      updateTimer.Enabled = false; 

      string version = "0"; 
      try 
      { 
       Version v = AssemblyName.GetAssemblyName("ParentApplication.exe").Version; 
       version = v.ToString(); 
      } 
      catch 
      { 
       version = "0"; 
      } 

      updateProgress(5); 

      DateTime buffer = DateTime.Now.AddMinutes(-5); 
      DateTime last = Convert.ToDateTime(configManager.AppSettings.Settings[lastCheck].Value); 
      int comp = DateTime.Compare(buffer, last); 

      if (comp < 0) 
      { 
       // Begin update process 
       updateApplication(version); 
      } 

      updateProgress(100); 

      System.Threading.Thread.Sleep(1000); 
     } 
     catch (Exception er) 
     { 
      logger.Error("Error in update application.", er); 
     } 

     // The updater can't update itself. Launch the external application 
     // to handle the updating of the updater. This application also launches 
     // the main executable. 
     Process sync = new Process(); 
     sync.StartInfo.UseShellExecute = false; 
     sync.StartInfo.FileName = "FinishUpdate.exe"; 
     sync.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
     sync.StartInfo.Verb = "runas"; 
     sync.Start(); 
     Application.Exit(); 
} 
+0

「懸掛」20秒感覺很像某種超時。你提到設備不在網絡上。是否有一些代碼(可能從開發中遺留下來?)試圖打開網絡資源? updateApplication()是否通過網絡檢查? – 2013-04-30 15:49:11

+0

它通過無線網卡連接到不在我們網絡上的服務器。有趣的是,表格在20秒內完全不顯示。一旦表格顯示出來,它就可以做到這一切。 – teynon 2013-04-30 15:50:28

+1

如果某些內容(如網絡調用...)阻塞了消息隊列,則表單不會顯示20秒。如果你做了一些需要一段時間的事情,可以在一個單獨的線程中完成(BackgroundWorker通常在WinForms上下文中運行良好)。 – 2013-04-30 15:52:00

回答

1

從描述中,分析器可以是難以設置。另一個選項是運行一個StopWatch,它將方法執行時間記錄到任意日誌文件。一旦確定哪種方法執行得不好,您可以重複這些步驟,直到縮小導致瓶頸的代碼行。