0

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);
user3077796
  • 192
  • 1
  • 19
  • How are you calling this "Shutdown" form? Is this your main form? – LarsTech Feb 19 '18 at 20:18
  • 1
    Seems a bit too late to the party. If the data is that important, then save it in a more timely way when you do have control, such as immediately upon receipt. You are giving up a lot of control when you try to do this in the death throes. Having said that, when does `FormClosing` not work? – DonBoitnott Feb 19 '18 at 20:33
  • It should save the log off time and event. How should I do that earlier? I couldn't find out when it does not work. Just noticed that there is sometime no log entry of the logout – user3077796 Feb 19 '18 at 20:36
  • How is `LogLogout` implemented? If this starts an async thread in background mode then the application is nog gonna wait for it when shutting down. – Jan-Peter Vos Feb 19 '18 at 20:56
  • No extra thread. It just calls an other object where the data is stored. – user3077796 Feb 19 '18 at 21:12
  • Why does the Microsoft.Win32.SystemEvents.SessionEnded += new Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded); not work? – user3077796 Feb 19 '18 at 21:12

1 Answers1

1

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();
        }
    }
user3077796
  • 192
  • 1
  • 19