4

I've got a problem with my application. I selected my Loadscreen.xaml as the "StartupUri" in my App.xaml. The Loadscreen.xaml.cs contains a progressbar, that runs until 100% - than it closes and opens the MainWindow. The problem is, that it opens the MainWindow twice after closing the Loadscreen. What is my fallacy?

App.xaml:

StartupUri="Loadscreen.xaml"
         Startup="Application_Startup">

Loadscreen.xaml.cs:

public void Timer_Tick(object sender, EventArgs e)
    {
        progressBar_Ladebalken.Value = i;
        label_Titel.Content = i + "%";
        Mouse.OverrideCursor = Cursors.Wait;

        if (i < 100)
        {
            i += 1;
        }
        else
        {
            i = 0;
            Mouse.OverrideCursor = null;
            Timer.Stop();

            Window W = new MainWindow();
            W.Show();

            this.Close();
        }

public void Application_Startup:

public void Application_Startup(object sender, StartupEventArgs e)
    {
        bool Absicherung;
        Mutex Mutex = new Mutex(true, this.GetType().GUID.ToString(), out Absicherung);

        if (Absicherung)
        {
            Window W = new Loadscreen();
            W.Closed += (sender2, args) => Mutex.Close(); ;
            W.Show();
        }
        else
        {
            MessageBox.Show(FM_Mutex_Meldung, FM_Mutex_Titelleiste, MessageBoxButton.OK, MessageBoxImage.Information);
            Mutex.Close();
            Application.Current.Shutdown();
        }
    }
sjantke
  • 605
  • 4
  • 9
  • 35

1 Answers1

15

You're opening two instances of Loadscreen:

  • One with StartupUri="Loadscreen.xaml"
  • The other one from Application_Startup which is called because of Startup="Application_Startup".

Just get rid of StartupUri="Loadscreen.xaml" and the problem should be gone.

Damir Arh
  • 17,637
  • 2
  • 45
  • 83
  • Thanks, it worked. But so, the MUTEX does not work anymore. If I run the application one more time, there is no error message that says that the application is already running. – sjantke Feb 01 '14 at 13:38
  • @Exception You did remove `StartupUri` and keep `Startup`? – Damir Arh Feb 01 '14 at 14:35
  • Yes. But it seems to work now... I'm just trying to fix some things. Thanks for your help. – sjantke Feb 01 '14 at 14:38
  • Amazing answer, had been searching for hours because I kept getting this stupid null reference somewhere in mscorelib defaultbinder... kept throwing that error after I added 2 services in constructor through DependencyInjection. I was looking at the wrong place I guess because the problem was that the StartupUri was pointing at a constructor of mainwindow.xaml without those injection probably.. removing that line fixed it. So Happy! Thanks! – Nick Cuypers Jun 18 '20 at 21:32