Results 1 to 10 of 10

Thread: file write based on time of the day

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    104

    file write based on time of the day

    guys,
    I'm developing a vb.net app, where i have to do some specific task like writing in a file and changing a variable (and hence application reloading) [actually changing the app operation mode and relaunching on the updated operation mode] based on a specific time of a day. Means, on 2AM daily, the app will do some task. And also on 6AM daily, the app will do another set of task. This can not be done by windows task scheduler but direct from the app. I need a guidance on how to achieve this?? I am thinking of using the system time and some other ways like timer polling for reading the time and on specific time defined, it will do the task. Any other better ideas, please advise me, so I can do that job!!!

    Mishu~

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: file write based on time of the day

    Using a timer is a good way to go, you can either set the interval so that it only fires at the specific time (which takes slightly less code, and slightly less resources), or you can have it check a bit more often (which avoids issues like daylight savings changes).

    Note however that if you are checking the time in the timer then you shouldn't be checking for it being exactly a specific time, but whether it is at least that time, because the timer might not fire at the exact second you want (because timers are low priority, and can therefore be delayed).

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    104

    Re: file write based on time of the day

    OK, but then i also need to check the CURRENT SYSTEM TIME right?? I mean a timer will fire to check the time and if it is that time (or a few minutes crossed) [like 4:00PM] then it will do the task. and the other time (same timer) will check the other TIMESTAMP for the same thing... right???
    at this time, i am showing the current time using a timer firing per second... changing the label text with values from system time. This will be same like right??

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: file write based on time of the day

    Yes, but you don't actually need to show the system time to compare it to the time you want to check.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    104

    Re: file write based on time of the day

    no no no... i'm not saying that... i'm saying that the same way as we do to show system time on app....

    there we use per second interval, and here we can avoid if that is not that mission critical right?

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: file write based on time of the day

    If you don't need it to the exact second then you can use a longer interval, and that would be better as it uses fewer resources.

  7. #7
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: file write based on time of the day

    Hi,

    here somthing you can perhaps use..

    you have to adjust the Times
    Code:
    Option Strict On
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Label1.Text = GetTimeOfDay(Now)
        End Sub
        Private Function GetTimeOfDay(ByVal TimeToDoSomething As Date) As String
    
            Select Case Format(TimeToDoSomething, "hh:nn:ss")
                Case "06:00:00" To "11:59:59"
                    GetTimeOfDay = "Morning"
                    'do something
                Case "12:00:00" To "15:59:59"
                    GetTimeOfDay = "Noon"
                    'do something
                Case "16:00:00" To "17:59:59"
                    GetTimeOfDay = "Afternoon"
                    'do something
                Case "18:00:00" To "21:59:59"
                    GetTimeOfDay = "Evening"
                    'do something
                Case Else
            End Select
        End Function
    
    End Class
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    104

    Re: file write based on time of the day

    hi chris!! nice.. but I'm a bit unsure if that may help...

    well, i have a subroutine that needs to run for once for the whole time (but surely have to run).
    Code:
    Public Sub changeOPSMode()
            If (TimeOfDay > #23:50:00#) Then
                opsMode = 3
                UpdateConfFile()
                ProcessOpsMode()
            ElseIf (TimeOfDay > #16:50:00#) Then
                opsMode = 1
                UpdateConfFile()
                ProcessOpsMode()
            End If
    This is my subroutine. Now as you can see, I have two times, on 23:50 I have to execute the two other subs ONLY for once. I cant use the equal sign ass it may fail if it misses the second for some reason (or I have to actually make a precise interval (per sec) timer and i am not going to to it... as i already have a timer for 30sec interval for doing another job and that is all enough.) and for other timeit's the same... any ideas??

  9. #9
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: file write based on time of the day

    Hi,

    try this, you still have to do a bit work with setting the Times

    when opening the Form the Action starts, I just set the two LogActions too a couple of seconds so
    you can see whats going on.

    what you still have to do is work out the Timespan from when starting (Date.Now)
    to the Time the Action is suposed to take place. I just HardCoded the 5 and 20sec. Actions

    put this in a Class..named clsCountdown
    Code:
    Public Class clsCountDown
    
        Private WithEvents m_Timer As New Timer
        Private m_CountDown As Date
        Public Event CountDown(ByRef NextCountDow As Date)
    
        Public Property IntervallCheck() As Integer
            Get
                Return m_Timer.Interval
            End Get
            Set(ByVal value As Integer)
                m_Timer.Interval = value
            End Set
        End Property
    
        Public Property NextCountDown() As Date
            Get
                Return m_CountDown
            End Get
            Set(ByVal value As Date)
                m_CountDown = value
            End Set
        End Property
    
        Public Sub New()
        End Sub
    
        Public Sub New(ByVal mNextCountDown As Date, ByVal mIntervallCheck As Integer)
            m_CountDown = mNextCountDown
            m_Timer.Interval = mIntervallCheck
            m_Timer.Start()
        End Sub
    
        Public Sub StartCheck()
            m_Timer.Start()
        End Sub
    
        Public Sub StopCheck()
            m_Timer.Stop()
        End Sub
    
        Private Sub m_Timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Timer.Tick
            If m_CountDown <= Date.Now Then
                Dim d As Date = Date.Now
                RaiseEvent CountDown(d)
                m_CountDown = d
            End If
        End Sub
    End Class
    put this in a Modul..
    this is where you add your Actions
    Code:
    Module Module1
        Public cCountDown1 As clsCountDown 'your Action No.1
        Public cCountDown2 As clsCountDown 'your Action No. 2
        'etc...
    End Module
    and this in a Form..
    you need 2 Listboxes, 3 Labels
    and add another 2 Foms , just so that you see an Action is fired
    Code:
    Public Class Form1
      
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim Log1 As Date = Date.Now
            Dim log2 As Date = Date.Now
    
            Log1 = Log1.AddSeconds(5)
            log2 = log2.AddSeconds(20)
    
            cCountDown1 = New clsCountDown(Log1, 1000)
            cCountDown2 = New clsCountDown(log2, 1000)
    
            AddHandler cCountDown1.CountDown, AddressOf Me.CountDown
            AddHandler cCountDown2.CountDown, AddressOf Me.CountDown2
    
        End Sub
    
    
        Private Sub CountDown(ByRef NextCountDown As Date)
            Label1.Text = Date.Now.ToString
            Dim Log1 As Date = Date.Now
    
            Log1 = Log1.AddSeconds(5)
            NextCountDown = Log1
            Form2.Show() 'your Action, in this case open a Form after the 5sec.
            ListBox1.Items.Add("Nextaction : " & Log1)
            Label2.Text = NextCountDown
    
        End Sub
        Private Sub CountDown2(ByRef NextCountDown As Date)
            Label1.Text = Date.Now.ToString
    
            Dim Log2 As Date = Date.Now
            Log2 = Log2.AddSeconds(20)
    
            NextCountDown = Log2
            Form3.Show()  'your Action, in this case open a Form after the 20sec.
            ListBox2.Items.Add("Nextaction : " & Log2)
            Label3.Text = NextCountDown
        End Sub
    End Class
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    104

    Re: file write based on time of the day

    Quote Originally Posted by ChrisE View Post
    Hi,

    try this, you still have to do a bit work with setting the Times

    when opening the Form the Action starts, I just set the two LogActions too a couple of seconds so
    you can see whats going on.

    what you still have to do is work out the Timespan from when starting (Date.Now)
    to the Time the Action is suposed to take place. I just HardCoded the 5 and 20sec. Actions

    put this in a Class..named clsCountdown
    Code:
    Public Class clsCountDown
    
        Private WithEvents m_Timer As New Timer
        Private m_CountDown As Date
        Public Event CountDown(ByRef NextCountDow As Date)
    
        Public Property IntervallCheck() As Integer
            Get
                Return m_Timer.Interval
            End Get
            Set(ByVal value As Integer)
                m_Timer.Interval = value
            End Set
        End Property
    
        Public Property NextCountDown() As Date
            Get
                Return m_CountDown
            End Get
            Set(ByVal value As Date)
                m_CountDown = value
            End Set
        End Property
    
        Public Sub New()
        End Sub
    
        Public Sub New(ByVal mNextCountDown As Date, ByVal mIntervallCheck As Integer)
            m_CountDown = mNextCountDown
            m_Timer.Interval = mIntervallCheck
            m_Timer.Start()
        End Sub
    
        Public Sub StartCheck()
            m_Timer.Start()
        End Sub
    
        Public Sub StopCheck()
            m_Timer.Stop()
        End Sub
    
        Private Sub m_Timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Timer.Tick
            If m_CountDown <= Date.Now Then
                Dim d As Date = Date.Now
                RaiseEvent CountDown(d)
                m_CountDown = d
            End If
        End Sub
    End Class
    put this in a Modul..
    this is where you add your Actions
    Code:
    Module Module1
        Public cCountDown1 As clsCountDown 'your Action No.1
        Public cCountDown2 As clsCountDown 'your Action No. 2
        'etc...
    End Module
    and this in a Form..
    you need 2 Listboxes, 3 Labels
    and add another 2 Foms , just so that you see an Action is fired
    Code:
    Public Class Form1
      
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim Log1 As Date = Date.Now
            Dim log2 As Date = Date.Now
    
            Log1 = Log1.AddSeconds(5)
            log2 = log2.AddSeconds(20)
    
            cCountDown1 = New clsCountDown(Log1, 1000)
            cCountDown2 = New clsCountDown(log2, 1000)
    
            AddHandler cCountDown1.CountDown, AddressOf Me.CountDown
            AddHandler cCountDown2.CountDown, AddressOf Me.CountDown2
    
        End Sub
    
    
        Private Sub CountDown(ByRef NextCountDown As Date)
            Label1.Text = Date.Now.ToString
            Dim Log1 As Date = Date.Now
    
            Log1 = Log1.AddSeconds(5)
            NextCountDown = Log1
            Form2.Show() 'your Action, in this case open a Form after the 5sec.
            ListBox1.Items.Add("Nextaction : " & Log1)
            Label2.Text = NextCountDown
    
        End Sub
        Private Sub CountDown2(ByRef NextCountDown As Date)
            Label1.Text = Date.Now.ToString
    
            Dim Log2 As Date = Date.Now
            Log2 = Log2.AddSeconds(20)
    
            NextCountDown = Log2
            Form3.Show()  'your Action, in this case open a Form after the 20sec.
            ListBox2.Items.Add("Nextaction : " & Log2)
            Label3.Text = NextCountDown
        End Sub
    End Class
    regards
    Chris
    Too much complex!!

    Still let me study it...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width