By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,333 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,333 IT Pros & Developers. It's quick & easy.

How do I properly exit...

P: n/a
I have an application that basically runs a timer and when it expires it
logs the user out, if the X button is clicked (Form.Close) the user is
logged out, but if I open TaskManager and go to the Processes tab and select
the application and say End Process none of my clean-up code (including user
logout) is executed. How can I force this to happen. BTW I'm using
'Inherits System.Windows.Forms.Form' in case that makes any difference

--

+-------------------------------------------------
| L0rd DarkF0rce - <l0***********@hotmail.com>
| "Time is the fire in which we burn..."
|
|AIM: PR Borg
|ICQ: 254445033
+-------------------------------------------------
Mar 29 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Here's the code in case it will make it easier...

Option Explicit On
Imports Microsoft.Win32
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents lblTimeLeft As System.Windows.Forms.Label
Friend WithEvents Timer1 As System.Windows.Forms.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.lblTimeLeft = New System.Windows.Forms.Label
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.GroupBox1.SuspendLayout()
Me.SuspendLayout()
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.lblTimeLeft)
Me.GroupBox1.Font = New System.Drawing.Font("Arial", 9.75!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))
Me.GroupBox1.Location = New System.Drawing.Point(16, 14)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(200, 75)
Me.GroupBox1.TabIndex = 0
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "Minutes Left Today"
'
'lblTimeLeft
'
Me.lblTimeLeft.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D
Me.lblTimeLeft.ForeColor = System.Drawing.Color.Red
Me.lblTimeLeft.Location = New System.Drawing.Point(50, 26)
Me.lblTimeLeft.Name = "lblTimeLeft"
Me.lblTimeLeft.TabIndex = 0
Me.lblTimeLeft.Text = "0"
Me.lblTimeLeft.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter
'
'Timer1
'
Me.Timer1.Interval = 60000
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(232, 102)
Me.Controls.Add(Me.GroupBox1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.ShowInTaskbar = False
Me.Text = "User Timer"
Me.GroupBox1.ResumeLayout(False)
Me.ResumeLayout(False)

End Sub
#End Region
' Define API functions
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As
Long, ByVal dwReserved As Long) As Long

' Define Global variables
Public iTimeLeft As Integer
Public DoW As String

' Define Global constants
Private Const ShutdownFlag As Long = &H14
Private Const ShutdownMsg As String = "No more time left for today.
Come back tomorrow..."
Private Const ShutdownTitle As String = "BYE"
Private Const DefSun As Integer = 120
Private Const DefMon As Integer = 30
Private Const DefTue As Integer = 30
Private Const DefWed As Integer = 30
Private Const DefThu As Integer = 30
Private Const DefFri As Integer = 120
Private Const DefSat As Integer = 120

Private Sub GetRegValues()
Dim hKey As RegistryKey

' Open the registry
hKey = Registry.CurrentUser.OpenSubKey("Software\UserTime r\")
' Read time left for today from registry
iTimeLeft = hKey.GetValue(DoW, "NONE")
' Close the registry
hKey.Close()
End Sub
Public Sub SaveValues()
Dim hKey As RegistryKey

' Open the registry
hKey = Registry.CurrentUser.OpenSubKey("Software\UserTime r\", True)
' Write updated value
hKey.SetValue(DoW, iTimeLeft)
' Close the registry
hKey.Close()
End Sub
Private Sub ResetYesterday()
Dim Yesterday As String
Dim hKey As RegistryKey
Dim iDef As Integer

Select Case Today.DayOfWeek
Case DayOfWeek.Sunday
Yesterday = "Saturday"
iDef = DefSat
Case DayOfWeek.Monday
Yesterday = "Sunday"
iDef = DefSun
Case DayOfWeek.Tuesday
Yesterday = "Monday"
iDef = DefMon
Case DayOfWeek.Wednesday
Yesterday = "Tuesday"
iDef = DefTue
Case DayOfWeek.Thursday
Yesterday = "Wednesday"
iDef = DefWed
Case DayOfWeek.Friday
Yesterday = "Thursday"
iDef = DefThu
Case DayOfWeek.Saturday
Yesterday = "Friday"
iDef = DefFri
End Select

' Open the registry
hKey = Registry.CurrentUser.OpenSubKey("Software\UserTime r\", True)
' Write updated value
hKey.SetValue(Yesterday, iDef)
' Close the registry
hKey.Close()
End Sub
Private Sub UserLogout()
MsgBox(ShutdownMsg, MsgBoxStyle.Information, ShutdownTitle)
'ExitWindowsEx(ShutdownFlag, 0)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Get day of week string
DoW = Today.DayOfWeek.ToString

'Load values from registry
GetRegValues()
If (iTimeLeft = 0) Then
UserLogout()
End If
lblTimeLeft.Text = iTimeLeft
ResetYesterday()
End Sub
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Closed
SaveValues()
UserLogout()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
' Update time left
iTimeLeft = iTimeLeft - 1

' Update display
lblTimeLeft.Text = iTimeLeft

' Save information to registry.
SaveValues()

If (iTimeLeft = 0) Then
UserLogout()
End If
End Sub
End Class
--

+-------------------------------------------------
| "Time is the fire in which we burn..."
|
|AIM: PR Borg
|ICQ: 254445033
+-------------------------------------------------
"L0rd DarkF0rce" <l0***********@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
I have an application that basically runs a timer and when it expires it
logs the user out, if the X button is clicked (Form.Close) the user is
logged out, but if I open TaskManager and go to the Processes tab and select the application and say End Process none of my clean-up code (including user logout) is executed. How can I force this to happen. BTW I'm using
'Inherits System.Windows.Forms.Form' in case that makes any difference

--

+-------------------------------------------------
| L0rd DarkF0rce - <l0***********@hotmail.com>
| "Time is the fire in which we burn..."
|
|AIM: PR Borg
|ICQ: 254445033
+-------------------------------------------------

Mar 29 '06 #2

P: n/a
I think you will find that "end process" does exactly that... it literally
kills the process stone dead, no tidying up, nothing.... It's the windows
version of "kill -9" for those that come from a *nix world..

Regards
Simon

--
================================
Simon Verona
Dealer Management Service Ltd
Stewart House
Centurion Business Park
Julian Way
Sheffield
S9 1GD

Tel: 0870 080 2300
Fax: 0870 735 0011

"L0rd DarkF0rce" <l0***********@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
I have an application that basically runs a timer and when it expires it
logs the user out, if the X button is clicked (Form.Close) the user is
logged out, but if I open TaskManager and go to the Processes tab and
select
the application and say End Process none of my clean-up code (including
user
logout) is executed. How can I force this to happen. BTW I'm using
'Inherits System.Windows.Forms.Form' in case that makes any difference

--

+-------------------------------------------------
| L0rd DarkF0rce - <l0***********@hotmail.com>
| "Time is the fire in which we burn..."
|
|AIM: PR Borg
|ICQ: 254445033
+-------------------------------------------------

Mar 30 '06 #3

P: n/a
Maybe I explained it wrong, but what I really want is to catch the process
terminate action when initiated from the processes tab in task manager, that
way I can clean my environment, save stuff to the registry and log the user
out, just like it does if the user clicks on the big X on the upper right
corner...
"Simon Verona" <no****@nomail.zzz> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I think you will find that "end process" does exactly that... it literally
kills the process stone dead, no tidying up, nothing.... It's the windows
version of "kill -9" for those that come from a *nix world..

Regards
Simon

--
================================
Simon Verona
Dealer Management Service Ltd
Stewart House
Centurion Business Park
Julian Way
Sheffield
S9 1GD

Tel: 0870 080 2300
Fax: 0870 735 0011

"L0rd DarkF0rce" <l0***********@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
I have an application that basically runs a timer and when it expires it
logs the user out, if the X button is clicked (Form.Close) the user is
logged out, but if I open TaskManager and go to the Processes tab and
select
the application and say End Process none of my clean-up code (including
user
logout) is executed. How can I force this to happen. BTW I'm using
'Inherits System.Windows.Forms.Form' in case that makes any difference

--

+-------------------------------------------------
| L0rd DarkF0rce - <l0***********@hotmail.com>
| "Time is the fire in which we burn..."
|
|AIM: PR Borg
|ICQ: 254445033
+-------------------------------------------------


Mar 30 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.