vb.net.net-3.0

Close form after 10 seconds


I have a windows form application that will open other forms, but will only display the forms for a few seconds (user configurable). I would normally do something like threading.thread.sleep(n), however when doing this the forms controls do not load only the white background shows, and I have also been reading that this isnt the best practice for what I am after as user input will not be actioned until the thread wakes up.

I have come across people using System.Timers.Timer(n), but I am struggling to get this to work for me, the form will only open and close straight away (you can only see a flash as the form opens then closes).

The code that I am using is:

Private Shared tmr As New System.Timers.Timer    
aForm.Show()
tmr = New System.Timers.Timer(aSleep * 60 * 60)
tmr.Enabled = True

aForm.Close()

This is all contained within a Private sub that passes the form and the defined run time.

My intention is to have the main application running from the task bar, which then calls one of the forms that will display for a defined period of time, close the form, then call another one of the forms.

Is any able to point me in the right direction for why the form opens then closes without seeing through the defined run time (I have been testing with 10 seconds), or is there a better way of doing what I am seeking?

Your help is greatly appreciated.

Matt


Solution

  • the docs say there's an Elapsed event handler that gets called when the time elapses. You would close the form in the handler:

    http://msdn.microsoft.com/en-us/library/system.timers.timer%28VS.85%29.aspx

    I just wrote a little example that shows what you would need to do at:

    http://www.antiyes.com/close-form-after-10-seconds

    Below is the relevant code, the full solution can be downloaded from the article.

    Form 1 code

    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim frm2 As New Form2()
            frm2.ShowDialog()
        End Sub
    
    End Class
    

    Form 2 code

    Imports System.Timers
    
    Public Class Form2
    
        Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
            MyBase.OnLoad(e)
            Dim tmr As New System.Timers.Timer()
            tmr.Interval = 5000
            tmr.Enabled = True
            tmr.Start()
            AddHandler tmr.Elapsed, AddressOf OnTimedEvent
        End Sub
    
        Private Delegate Sub CloseFormCallback()
    
        Private Sub CloseForm()
            If InvokeRequired Then
                Dim d As New CloseFormCallback(AddressOf CloseForm)
                Invoke(d, Nothing)
            Else
                Close()
            End If
        End Sub
    
        Private Sub OnTimedEvent(ByVal sender As Object, ByVal e As ElapsedEventArgs)
            CloseForm()
        End Sub
    
    End Class
    

    Of course for this code to work you'd need forms setup with the buttons.