Click here to Skip to main content
16,016,759 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friend i want to store the user log in and log out time to data base so how catch the user log out session time.

there user can also close the application from task manager :(

so don't know how to catch the application close event in wpf vb

What I have tried:

from login window i am inserted the log in time but don't know how to catch log out time... ?

Thanks in advance :)
Posted
Updated 5-Apr-18 21:04pm
v2

I know that I am a little late ... just got home.

As OriginalGriff stated, you can't catch all circumstances, but there are many that you can. Add the following to your Application class:
VB
Class Application

    Protected Overrides Sub OnStartup(e As StartupEventArgs)

        ' setup global exception handling  
        AddHandler Current.DispatcherUnhandledException,
            New DispatcherUnhandledExceptionEventHandler(AddressOf AppDispatcherUnhandledException)

        AddHandler Dispatcher.UnhandledException,
            New DispatcherUnhandledExceptionEventHandler(AddressOf DispatcherOnUnhandledException)

        AddHandler AppDomain.CurrentDomain.UnhandledException, _
                   AddressOf CurrentDomainOnUnhandledException

        ' start the app
        MyBase.OnStartup(e)

    End Sub

    Private Sub AppDispatcherUnhandledException(sender As Object, _
                                                e As DispatcherUnhandledExceptionEventArgs)
        ForwardUnhandledException(e)
    End Sub

    Private Sub DispatcherOnUnhandledException(sender As Object, _
                                               e As DispatcherUnhandledExceptionEventArgs)
        ForwardUnhandledException(e)
    End Sub

    Private Sub ForwardUnhandledException(e As DispatcherUnhandledExceptionEventArgs)
        ' forward the exception to AppDomain.CurrentDomain.UnhandledException ...
        Current.Dispatcher.Invoke(DispatcherPriority.Normal,
            New Action(Of Exception)(Sub(exc)
                                         Throw New Exception("Exception from another Thread", exc)
                                     End Sub), e.Exception)
    End Sub

    Private Sub CurrentDomainOnUnhandledException(sender As Object, _
                                                  e As UnhandledExceptionEventArgs)
        ' Do logging of exception details

        ' Let the user know that something serious happened...
        Dim ex = TryCast(e.ExceptionObject, Exception)
        MessageBox.Show(ex.Message,
                        ex.TargetSite.ToString(),
                        MessageBoxButton.OK,
                        MessageBoxImage.[Error])

        ' ask the app to shut down...
        Current.Shutdown()
    End Sub

    Protected Overrides Sub OnExit(e As ExitEventArgs)
        ' last change for cleanup code here!

        ' clear to exit app
        MyBase.OnExit(e)

    End Sub

End Class
 
Share this answer
 
Comments
Maciej Los 6-Apr-18 3:55am    
Late, but not lazy ;)
5ed!
Graeme_Grant 6-Apr-18 4:51am    
Thanks! :) Catches both clean and dirty exits...
You can't catch every exit: if the user disconnects the power for example, there is nothing you can do - and force closing the process from task manager has the same effect.

So do two things: handle the main form close event, and be prepared to find a "sensible default" when a user has not logged out.
 
Share this answer
 
Comments
ketan Ram Patil 6-Apr-18 2:34am    
thanks for your valuable replay have a gr8 day.. :)
OriginalGriff 6-Apr-18 2:48am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900