Before Shutdown wait till saving is finished

782 Views Asked by At

I have an application which logs some user data. If the users logs off the workstation it needs to save this information.

The program is a windows form c# application.

At the moment I am trying to do this by this function below and it works to 95% but not for a 100%.

    Application.Run(new Shutdown(_object));

    private void Shutdown_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = true;``
        _object.LogLogout(); //Save the logout to a file

    }

I tried as well the function but this didn't worked at all. And I don't understand why. Any hints? I am totally stuck here.

    Microsoft.Win32.SystemEvents.SessionEnded += new Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);
    Microsoft.Win32.SystemEvents.SessionEnding += new Microsoft.Win32.SessionEnding(SystemEvents_SessionEnding);
1

There are 1 best solutions below

0
On BEST ANSWER

Found the answer the shut-down will be only stopped if the form is still visible. This wasn't the case. Therefore I solved this by doing this.

    private void Shutdown_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (systemShutdown)
        {
            this.Show();
            //If GUI got closed delted active entry and log off
            _object.LogLogout(); //Save the logout to a file
            Thread.Sleep(100);
            this.Hide();
        }
    }