Question Timer Program Shutdown

griffithdesign

Active member
Joined
Sep 17, 2008
Messages
34
Programming Experience
Beginner
Alright im trying to make it where in Visual Basic 2008 you click on a box ether 10, 20, or 30. Then that tells a timer that the program needs to turn off in 10, 20, or 30 minutes. What I basically want it to do is, whenever the set amount time is when it goes off it shows form1(login form). I have prepared a picture can anyone supply me with some information on how to do this?

programprev1.jpg
 
You could use TimeSpan for this:
Class declaration:
VB.NET:
Private m_timer As TimeSpan
Private endTimer As String = ""
Set your timer tick for 1 sec. = 1000
In the tick event:
VB.NET:
m_timer = m_timer.Subtract(New TimeSpam(0,0,1))
endTimer = m_timer.ToString()
If endTimer = "00:00:00" Then
Me.Close
End If
get the value from the checkbox_checkchanged event,
VB.NET:
If ck10.checked = true then
m_timer = New TimeSpan(0,10,0) 'if 10 min check box:
End If
 
To allow a user to select a single option you can use RadioButton controls.

Have a place to store the alarm time:
VB.NET:
Private target As Date
in setbutton.Click event handler:
VB.NET:
If radio10.Checked Then
   target = Date.Now.AddMinutes(10)
ElseIf radio20.Checked Then
   '...
End If
OldTimer.Start()
in OldTimer.Tick event handler:
VB.NET:
If Date.Now >= target Then Me.Close()
The accuracy is determined by the timer interval, set it to 10000 if +-5 seconds is close enough.
 
The accuracy is determined by the timer interval, set it to 10000 if +-5 seconds is close enough.

Also bear in mind that timer events aren't guaranteed to fire if the code in the main thread is busy doing something else, so don't expect it to interrupt a long task that is running. If you are running something like that, you probably want multithreading - you can use System.Threading.Thread or the BackgroundWorker component.
 
so don't expect it to interrupt a long task that is running
Long tasks never run in main thread, if you do that you are making a programming error. This relates to any part of programming that deals with UI, and not this inquiry specifically.

It is though correct that if you happen to tie up the main thread then that work must be delegated to a secondary thread.

Also note that the time calculation is not affected by any minor delays of the timer event callback.
 
Also note that this is the Visual Studio general forum; i.e. it's for general questions about Visual Studio itself, such as "Where does Visual Studio get installed to?", "How can I customize the toolbars of visual studio?" or "What do the green and yellow bars on the left side of the code mean?"

Questions about how to write your own programs, would go in the VB.NET general discussion forum, not the VS.NET one :)
 
Thread moved to Windows Forms forum, please post in most appropriate forum.
 
or you could do something like this:


put this code in the button's click event:
VB.NET:
if chk10.checked = true then timer1.interval = 10000
if chk20.checked = true then timer1.interval = 20000
if chk30.checked = true then timer1.interval = 30000

put this in the timer's tick event:
VB.NET:
application.exit

*also you could consider using radio buttons insted of checkbox's, that way the user can't select more than one at a time.

let me know if it helps:)
 
joshuadeboer said:
timer1.interval = 10000
Is 10000 milliseconds really 10 minutes? (no)
also you could consider using radio buttons insted of checkbox's, that way the user can't select more than one at a time.
That's what I said.
 
VB.NET:
if chk10.checked = true then timer1.interval = 10000
if chk20.checked = true then timer1.interval = 20000
if chk30.checked = true then timer1.interval = 30000


Tell me, just out of curiosity, if you wanted to check whether a person was older than 18, would you do this:

VB.NET:
If (personAge > 18) = True Then ....

It's not quite so strange a question as it sounds, if you think about it
 
I was actually thinking more along the lines of

VB.NET:
If (((Not(Now.AddYears(-personAge)<=Now.AddYears(-18)))) And (Not False)) = True Then ...

I suppose we could also start using reflection to complicate this further, but I suspect this will do!
 
Sorry, my mistake it would be:

VB.NET:
if chk10.checked = true then timer1.interval = [B]600000[/B]
if chk20.checked = true then timer1.interval = [B]1200000[/B]
if chk30.checked = true then timer1.interval = [B]1800000[/B]
 
Sorry, my mistake it would be:

VB.NET:
if chk10.checked = true then timer1.interval = [B]600000[/B]
if chk20.checked = true then timer1.interval = [B]1200000[/B]
if chk30.checked = true then timer1.interval = [B]1800000[/B]

if chk10.checked then timer1.interval = 10 * 60 * 1000 'more self explanatory
 
Back
Top