Link to home
Start Free TrialLog in
Avatar of tvtech
tvtechFlag for Australia

asked on

Timer suspension/resume

Greetings,

I actually asked this question a week ago and thought that the answer would work, however, I neglected to test it on a midnight rollover. Here is the deal ...

I populate 2 x ComboBoxes with all times from 6:00am, 6:30am etc through to 12:00am. I get the user to select a Suspend time and a Resume time. Let's say that the user wants a Timer to stop at 5:00pm and resume at 6:00am. Entering something like;

If Format$(Now,"hh:nn ampm") > Suspend and Format$(Now,"hh:nn ampm")<Resume then
     'Timer_Enabled=False
else
     'Timer_Enabled=True
Endif

That code will not work as it does not take the midnight jump into account and therefore, the Suspend time will not be greater. Can someone please point me in the right direction.

Thank you.

Regards,
    Tony
Avatar of andysalih
andysalih

think your going to have to set a date in there somewhere

what you can do with a decision is

if time > suspend time and < resume time and date <> systemdate then

do what every

end if


cheers
andy
you can either store your date in a help variable and check on that too, when the date has changed store the new date, etc.

Or you can test on a datetime value, in which case you can always test on greater then.
Avatar of glass_cookie
Hi!

How about doing something like specifying the number of hours to 'hang' or suspend instead?

With respect to your question, what you can do is something like this: (Remember to declare the sleep Api)

Take note of the indexes (form the combo box) of the suspend time and resume time.

The difference in listindexes would be Resume - Suspend.  Thus, you could:

Sleep 30*60*(Resume-Suspend)
'Do the actions :)


That's it!

glass cookie : )
Avatar of tvtech

ASKER

Hi glass_cookie,

The Sleep API does not work for extended periods. Doing that would cause a "Not Responding" in the TaskList.

Regards,
   Tony
Hi tvtech

I hope like that may help
Note : the word Resume is a reserved word so I used REsumetime
   
    Dim SuspendTime As Date, ResumeTime As Date
    SuspendTime = Str(Date) & " " & combo1
    ResumeTime = Str(Date) & " " & combo2
    ResumeTime = IIf(ResumeTime < SuspendTime, 24 + ResumeTime, ResumeTime)
   
   
If Now > SuspendTime And Now < ResumeTime Then
    'Timer_Enabled=False
Else
    'Timer_Enabled=True
End If

Here is a wait function that does not use the API.
Try it and let me know.

Public Sub Wait(HowManySecs As Integer)
    On Error GoTo LocalError
'   pause for HowManySecs seconds
    Dim EndWait
    EndWait = DateAdd("s", HowManySecs, Now)
    While Now < EndWait
        'Do nothing while waiting
    Wend
Exit Sub
LocalError:
End Sub
You need to take date into account, so best to convert to date type

Dim suspend As String
Dim resumex As String

Dim datSus As Date
Dim datRes As Date


suspend = "05:00 pm"
resumex = "06:00 am"

'work out where the time should fall

datSus = CDate(suspend) + Int(Now)
datRes = CDate(resumex) + Int(Now)

If datSus < Now Then datSus = datSus + 1
If datRes < Now Then datRes = datRes + 1


'*****

then when you do the comparisson

If Now > datSus And Now < datRes Then
    'Timer_Enabled=False
Else
    'Timer_Enabled=True
End If


this will assume a midnight rollover when resume < suspend.
i assume that is what you want to achieve.

if resume < suspend then
  If Format$(Now,"hh:nn ampm") > Suspend and Format$(Now,"hh:nn ampm")<Resume then
    'Timer_Enabled=True
  else
    'Timer_Enabled=False
  Endif
else
  If Format$(Now,"hh:nn ampm") > Suspend and Format$(Now,"hh:nn ampm")<Resume then
    'Timer_Enabled=False
  else
    'Timer_Enabled=True
  Endif
end if

some recommendations:
- use the date data type to compare times, this is more solid (will work regardless of regional settings...)
- use a checkbox so that users can indicate wether they will be crossing midnight. This allows you to check whether their input is correct.

good luck,
francois

ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry...

If that causes a 'program is not responding' message, do this instead:

Take note of the indexes (form the combo box) of the suspend time and resume time.

The difference in listindexes would be Resume - Suspend.  

At the top:
Dim counttime as integer


In your event,

counttime = (Resume - Suspend)*30
Timer1.Enabled = True

In timer1,

If counttime = 0 Then
'Execute codes
Timer1.Enabled = False
Else
counttime = counttime - 1
End If

That's it!

glass cookie : )
Hi deighton
         I think I suggested same solution !!!
         Nice 2 meet U
Bahnass
Avatar of tvtech

ASKER

Thanks Ark. Yours handles the situation exactly as I'd hoped.

Thank you to everyone else for your replies.

Kind regards,
    Tony