0

I'm using Squirrel for installation and update process, that's why in my App.xaml.cs I handle custom Squirrel events

App.xaml.cs

public static event EventHandler ApplicationUpdated;
protected virtual void OnApplicationUpdated()
{
   ApplicationUpdated?.Invoke(this, EventArgs.Empty);
}

protected override void OnStartup(StartupEventArgs e)
{
  base.OnStartup(e);
  try
  {
      using (var mgr = new UpdateManager(updateUrl))
      {
          SquirrelAwareApp.HandleEvents(
            onInitialInstall: OnInitialInstall,
            onAppUpdate: OnApplicationUpdate,
            onAppUninstall: OnApplicationUninstall,
            onFirstRun: OnFirstRun);
      }
  }
  catch (Exception ex)
  { }
}

private void OnApplicationUpdate(Version version)
{
  using (var manager = new UpdateManager(updateUrl))
  {
      manager.CreateShortcutsForExecutable(appFullName, ShortcutLocation.Desktop, true);
      manager.CreateShortcutsForExecutable(appFullName, ShortcutLocation.StartMenu, true);
      manager.CreateShortcutsForExecutable(appFullName, ShortcutLocation.AppRoot, true);

      manager.RemoveUninstallerRegistryEntry();
      manager.CreateUninstallerRegistryEntry();
  }

  System.Windows.Forms.MessageBox.Show("OnApplicationUpdated"); // this mbox is showing up normally
  OnApplicationUpdated(); // here is where I fire event that notifies (or at least should notify) MainWindow about update
}

Update process and everything else (OnInitiallInstall etc) works correctly, but when I try to fire OnApplicationUpdated (it may be a bit confusing - it's my own static event) it doesn't fire(?) or MainWindow doesn't handle it properly, because method doesn't write single line in output nor changes any property of controls

MainWindow.xaml.cs

public MainWindow()
{
   // some code above

   App.ApplicationUpdated += App_ApplicationUpdated;

   // some code under
}

private void App_ApplicationUpdated(object sender, EventArgs e)
{
    Console.WriteLine("ApplicationUpdated");
    Dispatcher.Invoke(() => 
    {
        updateLabel.Visibility = Visibility.Collapsed;
        updateButton.Visibility = Visibility.Collapsed;
        updateProgressRing.Visibility = Visibility.Collapsed;

        restartButton.Visibility = Visibility.Visible;
    });
}

Could you tell me why is this happening, why MainWindow doesn't handle this event?

I've been using static events like that for quite long time and never had any similar issue

Steve
  • 61
  • 8
  • The issue may have to do with `this` in the Invoke... Try `ApplicationUpdated?.Invoke(null, EventArgs.Empty);` – Ron Beyer Apr 26 '18 at 21:29
  • The same results, I can see mbox, but nothing is printed out (in MainWindow) :c – Steve Apr 26 '18 at 21:54
  • Set a breakpoint in the `OnApplicationUpdated` method, and see if there are any subscribers to the event. – Ron Beyer Apr 26 '18 at 21:56
  • Weird thing, I've add Console.WriteLine("test") in OnApplicationUpdated (before invoke line) and even this isn't printed out – Steve Apr 26 '18 at 21:58
  • I've set breakpoints on OnApplicationUpdate and mbox line, just to be sure, and not to lie, breakpoints are never reached – Steve Apr 26 '18 at 22:02
  • It's a bad idea to put substantial code in onstartup. If anything goes wrong then the app will just shut down straight off. Do whatever you want to do after the first window is up and there. Invent a splash screen if necessary. – Andy Apr 27 '18 at 08:21
  • And then move all of this code to for example ctor of splash screen, create new instance of MainWindow in StartUpWindow and hide splash screen in some way, right? – Steve Apr 27 '18 at 08:25
  • I moved almost everything from App.xaml.cs to StartUpWindow (where I handle Squirrel custom events and create MainWindow instance) but results are the same, my own event isn't firing, all I can see is MessageBox – Steve Apr 28 '18 at 18:58

0 Answers0