TEStack
asked on
How to shut app down after 30 Min. VB.net 2005
How can I shut down my app 30 min. after it has been started?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Add a Timer to your main form. In the Form_Load event, set the timer's interval to 30*60*1000 (30 minutes), like this: Timer1.Interval = 30*60*1000.
Then add the following code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Application.Exit()
End Sub
Then add the following code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Application.Exit()
End Sub
You can also use a background thread and just put it to sleep:
Public Class Form1
Private AutoCloseThread As System.Threading.Thread
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AutoCloseThread = New System.Threading.Thread(AddressOf Sleeper)
AutoCloseThread.IsBackground = True
AutoCloseThread.Start()
End Sub
Private Sub Sleeper()
Dim ts As TimeSpan = TimeSpan.FromMinutes(30)
System.Threading.Thread.Sleep(ts.TotalMilliseconds)
Application.Exit()
End Sub
End Class
You have several ways of exiting your application:
1.) If you have only 1 working thread without long blocking calls you can simply check, if DateTime.Now.Ticks < StartTime.AddMinutes(30).T icks. If this is the case release all ressources and end your application. This is the softest way
2.) You can use the Timer and if it is elapsed call YourMainForm.Close. Then you can release all ressources and close, but this does not work, while the main GUI thread is not idle.
3.) You can start a new Thread that calls Thread.Sleep(30*60*1000) and then calls Application.Exit
4.) If you want to do it the hard way you can start your program from another program with Process.Start, then call Thread.Sleep(30*60*1000) afterwards and then uses Process.Kill to end the program. This will nearly always work, but you have no chance to release all ressources.
You can also use a combined solution to end the program the soft way after 30min and if it is not closed after 31min you can call Proces.Kill.
P.S.: I havent tried yet, but I think it is not possible to kill your own process.
Application.Exit() is a soft way of exiting your application. If you really want to be sure that it closes, you have to start it from another process, call Thread.Sleep(30*60*100) and call Process.Kill afterwards.
1.) If you have only 1 working thread without long blocking calls you can simply check, if DateTime.Now.Ticks < StartTime.AddMinutes(30).T
2.) You can use the Timer and if it is elapsed call YourMainForm.Close. Then you can release all ressources and close, but this does not work, while the main GUI thread is not idle.
3.) You can start a new Thread that calls Thread.Sleep(30*60*1000) and then calls Application.Exit
4.) If you want to do it the hard way you can start your program from another program with Process.Start, then call Thread.Sleep(30*60*1000) afterwards and then uses Process.Kill to end the program. This will nearly always work, but you have no chance to release all ressources.
You can also use a combined solution to end the program the soft way after 30min and if it is not closed after 31min you can call Proces.Kill.
P.S.: I havent tried yet, but I think it is not possible to kill your own process.
Application.Exit() is a soft way of exiting your application. If you really want to be sure that it closes, you have to start it from another process, call Thread.Sleep(30*60*100) and call Process.Kill afterwards.
ASKER
I believe that I did not ask the question properly. I actually need the VB App to close 30 min after a button is pressed. I've tried the above code under a button sub, but the timer never appeared to change.
ASKER
MartinHouse, I was finally able to make it work. Thanks for the help!
ASKER
I appreciate the help!
You're welcome. Pleased to have helped :-)
end if
Obviously, your application termination will need to be tidies to release any resources in use etc.
Hope this helps
Mart