• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

Prevent user from closing an application but let the system close it

Hi Experts..

I'm developing an application in vb.net thet needs to register computer usage time.

I must prevent user from closing the application and for that i used

Private Sub frm_Principal_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
       If not canClose Then
                  Me.WindowState = FormWindowState.Minimized
                 e.Cancel = True
       End If                                
    End Sub

canclose is a boolean tha i use to force application closing when true.

The problem is that when the user tries to log off or shut down the pc, windows tries to close the application. , the system stops the shutting down or logging of process  because canClose  is set to false.

I used the microsoft.Win32.SystemEvents.SessionEnding event to switch canClose  to True, but frm_Principal_Closing happens first so the SessionEnding event never fire.

please help
  • 3
1 Solution
My thoughts:

You are exactly right, the session ending will not fire in this case.  You are up poops creek w/ this implementation.  I have some ideas for you.  First, is the application form driven?  When it is open is the user doing anything with it or is it simply storing data?  It sounds to me like you are simply putting the exe in the startup menu and the application runs and stores data the whole time the computer is on, and when shut down happens the application needs to shut down nicely.

This is called a windows service.  A different type of project that you can do everything you want to w/ system data, that will start at start up and shut down at shut down time, and run in the background where no one can see or mess w/ it.

here is an article for creating windows services:


If there is an interface, i would then question what is necessary in the interface?  is it simply a reporting service for what the application is doing?  If you set your windows service up to store the data in a db or xml, you could have a second application much like an MMC snap in that controls the service and gives views to its data.

orugamalditaAuthor Commented:
thanks... i will be evalluating this option for a while...

hope it works, you'll be getting your points...

orugamalditaAuthor Commented:
Do you (or anyone) know if i can run a windows service but for the current windows user.. Because my application needs to catch information about the active window. i managed to create a windows service but runs as "System" and can't catch that information.
Mike TomlinsonMiddle School Assistant TeacherCommented:
See here:

It demonstrates how to differentiate between a USER initiated close (via the X button in the top right) or if something else is shutting it down.
orugamalditaAuthor Commented:
THAT WORKED!!! ... THANKS Idle_Mind ...

 Thank you too mydasx... it was to much work to transform my applictaion to the windows service and the interface. I learned a lot about windows services thoug


Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now