C#/.NET Constructor of WPF-Window is called twice when opened from Mainwindow

79 views Asked by At

i have an application which starts with a Mainmenu integrated into the default MainWindow. That works perfectly fine, but now i have to create an automatic dataupdate function which should update the data before the application shows the MainWindow.

This works also fine, but i noticed that at the loading/initializing of the MainWindow my update-window is called twice and therefore the update-function is also called twiced. The Application also runs the constructors of the called Autoupdater-Window twice.

The code of the MainWindow.xaml.cs:

public MainMenuView()
{
    // Instance needed classes
    DatabaseLock _databaseLock = new DatabaseLock();
    Utils _util = new Utils();
    Eventlog _eventlog  = new Eventlog();

    // Check if auto update had already run today
    if (Settings.Default.LastJSONUpdate.Date == DateTime.Now.Date)
    {
        // Write error to the eventlog
        _eventlog.WriteEventLog("MainMenuView()->MainMenuView.xaml.cs", "foo", _util.GetCurrentUser(), DateTime.Now, Eventlog.Types.INFO);
        // Show info message to the user
        System.Windows.Forms.MessageBox.Show("Dataupdate already runned today!", "foo", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else
    {
        // Create lock for table
        _databaseLock.CreateLock("autoupdate", _util.GetCurrentUser());

        // Check if the autoupdate-window is already open and if not open the updater
        if (!_util.CheckIfWinowIsOpened("AutoUpdate"))
        {
            // Minimize the mainmenu window
            _util.MinimizeMaximizeMainMenu();

            // Create new instance of AutoUpdaterView
            View.AutoUpdaterView autoUpdaterView = new View.AutoUpdaterView();
            // Show the window
            autoUpdaterView.Show();

            // Disable the mainwindow
            System.Windows.Application.Current.Windows[0].IsEnabled = false;
        }
    }

    InitializeComponent();
}

I tried to move the code to the cs-File of the UserControl itself, but that didn't helped, the result was the same.

So i hope somebody can bring some light to my thoughts.

Thanks in advance GeoCoder

1

There are 1 answers

0
GeoCoder On BEST ANSWER

I've tried all solutions suggested in the Comments.

The solution that worked for me was to create a own UserControl für the Update-Screen and show it via popup at the MainWindow. Now all Messages are loaded only once.

Thanks for all your suggestions and comments!