10秒后关闭表格

时间:2010-12-22 03:54:38

标签: vb.net .net-3.0

我有一个Windows窗体应用程序,它将打开其他窗体,但只显示窗体几秒钟(用户可配置)。我通常会做类似threading.thread.sleep(n)的事情,但是当这样做时,表单控件不会仅加载白色背景节目,而且我也一直在阅读这不是我作为用户所做的最佳实践在线程唤醒之前,不会对输入进行操作。

我遇到过使用System.Timers.Timer(n)的人,但是我很难让这个为我工作,表单只会立即打开和关闭(你只能在表单打开时看到一个flash然后关闭)。

我使用的代码是:

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

aForm.Close()

这都包含在传递表单和定义的运行时的Private子中。

我的目的是让主应用程序从任务栏运行,然后调用其中一个将在指定的时间段内显示的表单,关闭表单,然后调用另一个表单。

是否能够指出我正确的方向为什么表格打开然后关闭而没有看到定义的运行时间(我已经测试了10秒),或者有更好的方法来做我正在寻找的东西?

非常感谢您的帮助。

马特

6 个答案:

答案 0 :(得分:7)

文档说有一个Elapsed事件处理程序在时间流逝时被调用。您将在处理程序中关闭表单:

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

我刚刚写了一个小例子,展示了你需要做什么:

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

以下是相关代码,完整的解决方案可以从文章中下载。

表单1代码

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

表单2代码

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

当然,要使此代码正常工作,您需要使用按钮设置表单。

答案 1 :(得分:2)

您的代码设置了一个计时器,然后立即关闭表单。必须在计时器事件触发时完成关闭。

答案 2 :(得分:2)

我想我可以稍微扩展Jonathan的答案。

在您希望显示给定时间的表单上,添加一个计时器(在此示例中,计时器名为Timer1 ...可以在工具栏中找到计时器,只需将其拖到表单上)

要在表单显示一段给定时间后关闭表单,请在表单的onload方法中启动计时器:

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Do initialization stuff for your form...
    'Start your timer last.
    Timer1.Start()
End Sub

这将启动您的计时器。当预设时间过去时,勾选事件将触发。在此活动中,请填写表单结束代码:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    'Close the form after 1 tick.
    Me.Close()
End Sub

要更改计时器计时之前应经过的时间,请更改计时器间隔属性。

'Changing the time from outside the Form1 class...
Form2.Timer1.Interval = 2000 '2 seconds, the interval is in milliseconds.

完整代码,form1有一个按钮,用于设置计时器间隔,然后打开form2。

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form2.Timer1.Interval = 2000
        Form2.Show()
    End Sub
End Class


Public Class Form2
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Do initialization stuff for your form...
        'Start your timer last.
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.Close()
    End Sub
End Class

我希望这会有所帮助,让我知道我能否以更清晰的方式重述任何事情: - )

答案 3 :(得分:1)

只需在表单上粘贴一个时间组件即可启用它。在tick事件处理程序中,确定表单打开后的时间以及预期的时间段是否已经过去,关闭表单。

通过在等待Tick事件时允许Form_Load句柄返回,允许表单绘制并执行通常会执行的所有操作。

虽然你可以像你一样从代码创建时间,但我不确定你为什么会这样做。你肯定需要为Tick事件设置一个处理程序,以便它做任何好事。

答案 4 :(得分:0)

如果frmTemperatureStatus关闭,在设定时间(在此示例中)打开表单的一种真正简单方法将跳过计时器。我打开frmTemperatureStatus作为普通表单而不是对话框,否则代码会跳转到此表单,并且在表单关闭之前不会返回。 DoEvents保持frmTemperatureStatus响应(注意:如果逐行测试代码,frmTemperatureStatus将继续失去焦点,因为焦点会继续回到Visual Studio)。

            timeIncubation_End_Time = Now.AddMinutes(1)
            Me.Enabled = False
            frmTemperature_Status.Show()

            Do While frmTemperature_Status.Visible And timeIncubation_End_Time > Now
                Application.DoEvents()
                Threading.Thread.Sleep(100)
            Loop
            frmTemperature_Status.Close() ' This line doesn't cause an error if the form is already closed
            Me.Enabled = True
            MsgBox("End of dialog test")

答案 5 :(得分:0)

Public Class Form1
    Dim second As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 1000
        Timer1.Start() 'Timer starts functioning
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = DateTime.Now.ToString

        second = second + 1
        If second >= 10 Then
            Timer1.Stop() 'Timer stops functioning
            Me.Close()
            MsgBox("Timer Stopped....")
        End If

    End Sub

End Class