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

Posted on 2006-05-26
Last Modified: 2008-02-01
Hi Experts..

I'm developing an application in 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
Question by:orugamaldita
    LVL 5

    Expert Comment

    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.


    Author Comment

    thanks... i will be evalluating this option for a while...

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


    Author Comment

    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.
    LVL 85

    Accepted Solution

    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.

    Author Comment

    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

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    This video discusses moving either the default database or any database to a new volume.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now