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!
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]