Shutdown Timer

cpeterson36

Member
Joined
Apr 23, 2015
Messages
7
Programming Experience
10+
I have an application that is supposed to shut itself down on Sunday between 9 and 10 am. It does not seem to work. If I run the program and sit and watch it, it will shut down as it is expected. This application runs across a network and there may be 100 instances of it open at any one time. I need the shutdown so I can push updates so all the PC shortcuts will remain intact.

It first checks if it is Sunday then checks if the time is between 9 and 10 am. If that is all true, then it checks a SQL table to see if the switch is on to shut it down. This is added in case we are working a Sunday and don't want it to shut down. Then if it is off, I execute Me.Dispose(). I tried Application.Exit but that threw an error.

My current theory is that because the programs runs and everyone heads home on Friday night, the PCs go into hibernation and that stops it from shutting down on Sunday morning. Is that a valid thought?

Here is my code. 2 blocks. One activates timer and the second handles the timer ticks. This code is in the Main start up form. There are 15 other forms in this application but the timer is public and should keep running. There are no long running tasks that would block out the shut down time.

Thank you in advance for your assistance!

VB.NET:
[SIZE=2]    
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] frmStart_Activated([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Activated[/SIZE]
[SIZE=2]       
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ShutDownTimer[/SIZE]
[SIZE=2]            .Interval = 30000
            .Enabled = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]        
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]        
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Refresh()[/SIZE]
[SIZE=2]    
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE]

VB.NET:
[SIZE=2]     Public Sub ShutDownTimer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles ShutDownTimer.Tick
        Try
            'Timer will shut down program every Sunday between 9 and 10 am.
            If DayOfWeek.Sunday Then
                'If DayOfWeek.Sunday Then
                Me.Refresh()
                Dim currentTime As TimeSpan = DateTime.Now.TimeOfDay
                'Shutdown Time Start set to 9 am
                Dim shutdownTimeStart As TimeSpan = New TimeSpan(9, 0, 0)
                'Shutdown Time End set to 10 am
                Dim shutdownTimeEnd As TimeSpan = New TimeSpan(10, 0, 0)
                If currentTime > shutdownTimeStart And currentTime < shutdownTimeEnd Then
                    Dim blnTimerSwitch As Boolean = False
                    'Check that timer switch is on
                    Dim myconn0 As New SqlConnection("Data Source=W108T-SQL01;Initial Catalog=SupplyChain;Integrated Security=True")
                    Dim strSQL As String = "SELECT Stitch_Factor FROM dbo.XP_PZ_Data_Capture_Op_Codes Where Two_Letter_Code = '* '"
                    Dim myCmd0 As New SqlClient.SqlCommand
                    With myCmd0
                        .CommandType = CommandType.Text
                        .Connection = myconn0
                        .CommandText = strSQL
                        .CommandTimeout = 0
                    End With
                    myconn0.Open()
                    Dim sdr0 As SqlDataReader = myCmd0.ExecuteReader
                    While sdr0.Read
                        blnTimerSwitch = sdr0(0)
                    End While
                    myconn0.Close()
                    If blnTimerSwitch = "True" Then
                        'Switch is ON so close application
                        Me.Dispose()
                    End If
                End If
            Else
                'Switch is OFF
                'Do Nothing
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error in Shutdown Timer", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
[/SIZE]
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
I just want the application to shut down.
Applications (programs) don't "shut down", the system can though... so I'm assuming that you want your application to exit? No?

How does your application normally close? Do you have a routine that gets called to cleanup any data or objects? Do you just close the main form (the startup form)?
In most cases you can just use Me.Close() on the startup form.
 

cpeterson36

Member
Joined
Apr 23, 2015
Messages
7
Programming Experience
10+
Sorry for my misquote. Yes, I want the Application to Exit.

I have a Me.Close(). The problem seems to be that it won't actually close like a want it to close. Not sure why.
 

cpeterson36

Member
Joined
Apr 23, 2015
Messages
7
Programming Experience
10+
No the Form closing is very simple:


VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] frmStart_FormClosing([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Windows.Forms.FormClosingEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].FormClosing[/SIZE]
[SIZE=2]
        
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Dispose()[/SIZE]
[SIZE=2]
    
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][/COLOR][/SIZE][/COLOR][/SIZE]
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
No need for that code, since you're running that from the form itself it can't actually dispose of itself. Unless you're doing anything else in your program you don't have reason to use the FormClosing event at all.
 

cpeterson36

Member
Joined
Apr 23, 2015
Messages
7
Programming Experience
10+
That code is needed. It is an application that does many things and has 16 other forms that can be pulled up by the user. I am only trying to get it to exit between 9 and 10 am on Sunday morning. This code is for the user to exit the program by choice.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
That code is needed. It is an application that does many things and has 16 other forms that can be pulled up by the user. I am only trying to get it to exit between 9 and 10 am on Sunday morning. This code is for the user to exit the program by choice.
But the FormClosing only happens when the program is exiting (on the startup form) and since it's not possible to dispose of an object from itself it's code that doesn't even work, so no, you don't need that and having it there might be source of your exiting app problem(s)
 

cpeterson36

Member
Joined
Apr 23, 2015
Messages
7
Programming Experience
10+
I see what you are saying. I changed the code to Application.Exit() and will test that overnight. Thank you for your help. I will let you know how it goes!
 

cpeterson36

Member
Joined
Apr 23, 2015
Messages
7
Programming Experience
10+
That seems to have made it work. Thank you for your assistance!

This thread is resolved. Not sure how to mark it closed or answered....
 
Last edited:
Top Bottom