Hi, I am trying to make a program, what it needs to do is wait for 30 seconds with the form fully working, so you can not notice you are being timed, then after 30 seconds you will get an alert like

MsgBox ("Time's up!")

Thanks.

TomW commented: Using a timer is the answer to your question... +0
kvprajapati commented: N/A +6

Recommended Answers

All 3 Replies

Use a timer....

commented: Sarcastic. Never used a timer before, quite new to VB +0
commented: You don't deserve -rep for a good solution +5

What you want to do open a form or close a form or just show a message box ...
First add a timer and change its Enabled property to true.
Then adjust time to 30 seconds ( 1000 is equal to 1 second , so set 30000) .
Add this code in timer :

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        MsgBox("Times Up !")
    End Sub

Or if you want to check it only for first time then

Public Class Form1
    Dim timerflag As Integer
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If timerflag = 0 Then
            MsgBox("Times Up !")
            timerflag = 1
        Else
            Timer1.Enabled = False
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        timerflag = 0
    End Sub
End Class

If you want to close the form after 30 seconds then Use END before else.

Here is another way:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Dim act As Action(Of Integer) = AddressOf ShowMessageBoxAfter
		act.BeginInvoke(30, Nothing, Nothing)	'Waits 30 seconds
	End Sub

	Private Sub ShowMessageBoxAfter(ByVal sleep As Integer)
		System.Threading.Thread.Sleep(sleep * 1000)
		MessageBox.Show("Hi")
	End Sub

That calls the thread.sleep on another thread before showing the message box.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.