diff options
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Updater/MainWindow.xaml.cs')
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.Updater/MainWindow.xaml.cs | 174 |
1 files changed, 30 insertions, 144 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Updater/MainWindow.xaml.cs b/Software/Visual_Studio/PPC/Tango.PPC.Updater/MainWindow.xaml.cs index 8f521c85a..ae4f22420 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Updater/MainWindow.xaml.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Updater/MainWindow.xaml.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; -using System.Security.Principal; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -27,17 +26,16 @@ namespace Tango.PPC.Updater private String _sourceFolder = AppDomain.CurrentDomain.BaseDirectory; private String _msProcessName = "Tango.PPC.UI"; private String _appPath; - private bool EMULATE_EXCEPTION = false; public MainWindow() { //Launch debugger.. -#if DEBUG - if (!Debugger.IsAttached) - { - Debugger.Launch(); - } -#endif + //#if DEBUG + // if (!Debugger.IsAttached) + // { + // Debugger.Launch(); + // } + //#endif InitializeComponent(); @@ -58,16 +56,8 @@ namespace Tango.PPC.Updater Width = touch_screen.Bounds.Width; Height = touch_screen.Bounds.Height; } - else - { - Top = 0; - Left = 0; - } ContentRendered += MainWindow_ContentRendered; - - btnRetry.Click += BtnRetry_Click; - btnAbort.Click += BtnAbort_Click; } private void MainWindow_ContentRendered(object sender, EventArgs e) @@ -79,32 +69,7 @@ namespace Tango.PPC.Updater { try { - ShowProgress(); - - if (!IdentityUtils.IsElevated()) - { - ShowError("The updater utility is not running under elevated permissions and cannot perform.\nThe process will restart."); - var exeName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; - ProcessStartInfo startInfo = new ProcessStartInfo(exeName); - startInfo.Arguments = String.Join(" ", App.StartupArgs); - startInfo.Verb = "runas"; - Process.Start(startInfo); - Environment.Exit(0); - return; - } - } - catch { } - - try - { Init(); - - if (EMULATE_EXCEPTION) - { - EMULATE_EXCEPTION = false; - throw new InvalidOperationException("This is an emulated error."); - } - EnsureTangoIsDown(); RemoveOldDLLFiles(); ReplaceFiles(); @@ -113,31 +78,16 @@ namespace Tango.PPC.Updater DoEvents(); Thread.Sleep(1000); StartTango(true); - Exit(); } catch (Exception ex) { - ShowFailed(ex); + ShowError($"Update failed.\n{ex.Message}"); + StartTango(false); } - } - - private void Exit() - { - try + finally { - foreach (var file in Directory.GetFiles(_sourceFolder, "*.*", SearchOption.AllDirectories)) - { - try - { - File.Delete(file); - } - catch { } - } + Environment.Exit(0); } - catch { } - - - Environment.Exit(0); } private void Init() @@ -145,11 +95,6 @@ namespace Tango.PPC.Updater try { _appPath = String.Join(" ", App.StartupArgs); - - if (!_appPath.EndsWith("\\")) - { - _appPath += "\\"; - } } catch { @@ -173,18 +118,12 @@ namespace Tango.PPC.Updater { p.StartInfo.Arguments = "-update_ok"; } - else - { - p.StartInfo.Arguments = "-update_failed"; - } p.Start(); } private void ReplaceFiles() { - txtStatus.Text = "Updating files..."; - int maxProgress = Directory.GetFiles(_sourceFolder, "*.*", SearchOption.AllDirectories).Length; int progress = 0; @@ -204,6 +143,7 @@ namespace Tango.PPC.Updater { try { + txtStatus.Text = "Copying file " + Path.GetFileName(newPath); DoEvents(); File.Copy(newPath, newPath.Replace(_sourceFolder, _appPath), true); @@ -211,6 +151,8 @@ namespace Tango.PPC.Updater prog.Maximum = maxProgress; prog.Value = progress++; DoEvents(); + + Thread.Sleep(10); } catch (Exception ex) { @@ -243,49 +185,37 @@ namespace Tango.PPC.Updater { Process appProcess = null; - for (int i = 0; i < 20; i++) + int tries = 0; + + do { - try - { - appProcess = Process.GetProcessesByName(_msProcessName).FirstOrDefault(); - } - catch - { - Thread.Sleep(1000); - continue; - } + appProcess = Process.GetProcessesByName(_msProcessName).FirstOrDefault(); + Process p = new Process(); + p.StartInfo.CreateNoWindow = true; + p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; + p.StartInfo.FileName = "wmic"; + p.StartInfo.Arguments = String.Format("process where name='{0}' delete", _msProcessName); if (appProcess != null) { - try - { - appProcess.Kill(); - } - catch { } + tries++; + appProcess.Kill(); try { - Process p = new Process(); - p.StartInfo.CreateNoWindow = true; - p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; - p.StartInfo.FileName = "wmic"; - p.StartInfo.Arguments = String.Format("process where name='{0}' delete", _msProcessName); p.Start(); } catch { } + + Thread.Sleep(1000); } - else + + if (tries > 10) { - break; + throw new IOException("The main Tango process seems to in a frozen state. Please restart your computer and try again."); } - Thread.Sleep(1000); - } - - if (appProcess != null) - { - throw new IOException("The main Tango process seems to in a frozen state. Please restart your computer and try again."); - } + } while (appProcess != null); } /// <summary> @@ -300,49 +230,5 @@ namespace Tango.PPC.Updater { MessageBox.Show(error, "Tango Update", MessageBoxButton.OK, MessageBoxImage.Error); } - - private bool ShowErrorRetry(String error) - { - var result = MessageBox.Show(error + "\n" + "Press yes to retry.", "Tango Update", MessageBoxButton.YesNo, MessageBoxImage.Error); - return result == MessageBoxResult.Yes; - } - - private void ShowProgress() - { - stackProgress.Visibility = Visibility.Visible; - stackFailed.Visibility = Visibility.Collapsed; - } - - private void ShowFailed(Exception ex) - { - try - { - using (EventLog eventLog = new EventLog("Application")) - { - eventLog.Source = "PPC Updater"; - eventLog.WriteEntry($"PPC Updater Failed\n{ex.ToString()}", EventLogEntryType.Error, 101, 1); - } - } - catch { } - - stackProgress.Visibility = Visibility.Collapsed; - stackFailed.Visibility = Visibility.Visible; - txtError.Text = ex.Message; - } - - private void BtnAbort_Click(object sender, RoutedEventArgs e) - { - ShowProgress(); - txtStatus.Text = "Update failed. Restoring previous application state..."; - DoEvents(); - Thread.Sleep(1000); - StartTango(false); - Exit(); - } - - private void BtnRetry_Click(object sender, RoutedEventArgs e) - { - Update(); - } } } |
